BantexRef1788
BantexRef1788

Reputation: 31

Can't open file from IE when using content-disposition attachment and very long file name

I'm using very simple code to download file from ASP .NET web-application. The problem is in Internet Explorer when file name length is 134 symbols and more. The standard dialog box is shown ("Do you want to open or save 123456789012345678901234567890123456....pdf from localhost?"). But when clicking "Open" button nothing happens. There is no problem, when file name length is shorter, i.e. 133.

My code:

string fileName = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.pdf";
byte[] fileData = File.ReadAllBytes(Server.MapPath("~/document.pdf"));           

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
Response.OutputStream.Write(fileData, 0, fileData.Length);
Response.Flush();
Response.End();

Upvotes: 3

Views: 1675

Answers (1)

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31641

Chrome is smart enough to truncate the filename when the full path exceeds 251 chars. On Windows, Chrome saves to C:\users\<username>\Downloads by default which enables much larger files.

IE 11 approach is to just skip the Open/Save clicks and make Cancel your only valid choice without informing the user why. IE prevents longer filenames since it saves to an already long path C:\Users\<username>\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\XXXXXXXX.

Approach #1: Server-Side Truncation

If you have control over the attachment filename in the HTTP Response, and the client is IE, you should truncate the attachment filename to some MAX # of chars (accounting for default IE save location).

const int MaxFilenameLength = 140;
string fileName = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.pdf";
bool isIEBrowser = Request.Browser.Browser == "IE" || Request.Browser.Browser == "InternetExplorer";
if (fileName.Length > MaxFilenameLength && isIEBrowser)
    fileName = fileName.Substring(0, MaxFilenameLength); 
\\...
Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
\\...

Approach #2: Client-Side Default Save Path

If you don't have control over the server, you will need to change the default save location in IE to a shorter path, perhaps using what Chrome uses --> C:\users\<username>\Downloads. Go to IE Internet Options-->Settings-->Move Folder...

enter image description here

MS IE team needs to fix this bug as the user is left guessing what to do. To me, this is a client-side (IE 11) problem.

Upvotes: 2

Related Questions