سؤال

I am using asp.net mvc3, download file in the same browser (Chrome 22). Here is the controller code:

[HttpPost]
public ActionResult Uploadfile(HttpPostedFileBase file)//HttpPostedFileBase file, string excelSumInfoId)
{
    ...
    return File(
        result.Output,
        "application/vnd.ms-excel",
        String.Format("{0}_{1:yyyy.MM.dd-HH.mm.ss}.xls", "Суммирование", DateTime.Now));
}

On my dev machine I download a programmatically created file with the correct name "Суммирование_2012.10.18-13.36.06.xls".

Response:

Content-Disposition:attachment; filename*=UTF-8''%D0%A1%D1%83%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5_2012.10.18-13.36.06.xls
Content-Length:203776
Content-Type:application/vnd.ms-excel
Date:Thu, 18 Oct 2012 09:36:06 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0

And from production server I download a file with the name of the controller's action + correct extension "Uploadfile.xls", which is wrong.

Response:

Content-Disposition:attachment; filename="=?utf-8?B?0KHRg9C80LzQuNGA0L7QstCw0L3QuNC1XzIwMTIuMTAuMTgtMTMuMzYu?=%0d%0a =?utf-8?B?NTUueGxz?="
Content-Length:203776
Content-Type:application/vnd.ms-excel
Date:Thu, 18 Oct 2012 09:36:55 GMT
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET

Web.config files are the same on both machines.

Why does filename gets encoded differently for the same browser? Are there any kinds of default settings in web.config that are different on machines that I am missing?

هل كانت مفيدة؟

المحلول

The dev server is running .NET 4, and the production server is running .NET 4.5. The MVC framework contains a heuristic for determining whether it needs to use RFC 6266 for the Content-Disposition header, and while this heuristic works correctly on .NET 4 it does not work correctly on .NET 4.5. The end result is that the Content-Disposition header gets mangled, as you're witnessing in this instance.

Your easiest course of action would probably be to upgrade the application to MVC 4. That version of the framework contains a different heuristic that is more robust and should work correctly on both .NET 4 and .NET 4.5.

نصائح أخرى

Most likely reason seems to be that the server indeed sees different User-Agent header fields.

That being said, the 2nd response isn't correct for any browser, and you should report that problem to Microsoft.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top