Can't open file from IE when using content attachment and very long filename

I am using very simple code to upload a file from ASP.NET web application. Problem in Internet Explorer when filename is 134 characters or more. A standard dialog box is displayed ("Do you want to open or save 123456789012345678901234567890123456 .... pdf from localhost?"). But when I click the Open button, nothing happens. No problem when the length of the filename 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();

      

+3


source to share


1 answer


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

, which allows for significantly larger files.

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

.

Approach # 1: Truncate on the server side

If you have control over the attachment file in the HTTP response, and the client is IE, you should truncate the attachment file name to some MAX # characters (considering IE's default 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 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 -> Preferences -> Move Folder ...

enter image description here

The MS IE team needs to fix this error, as the user has to guess what to do. For me this is a client side issue (IE 11).

+2


source







All Articles