当使用内容配置附件和很长的文件名时,不能从IE打开文件

时间:2022-08-01 18:14:08

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.

我使用非常简单的代码从asp.net web应用程序下载文件。当文件名称长度为134个符号时,问题就出现在Internet Explorer中。标准对话框显示(“你想打开或保存123456789012345678901234567890123456 ....从localhost pdf ?”)。但是当点击“打开”按钮时,什么也没有发生。当文件名长度较短时(即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();

1 个解决方案

#1


2  

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.

当完整路径超过251 chars时,Chrome足够聪明,可以截断文件名。在Windows系统上,Chrome保存到C:\用户 <用户名> \下载默认为支持更大的文件。

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.

IE 11的方法是跳过打开/保存点击,让取消您唯一有效的选择,而不告诉用户为什么。即防止长文件名,因为它节省了一个已经长路径C:\Users\ <用户名> \ AppData \当地\微软\ Windows \ \ Content.IE5 \ XXXXXXXX Internet临时文件。

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).

如果您控制了HTTP响应中的附件文件名,并且客户端是IE,那么您应该将附件文件名截断到最大字符号(考虑到默认的IE保存位置)。

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...

如果您无法控制服务器,那么您需要将IE中默认的保存位置更改为更短的路径,或者使用Chrome使用的——> C:\users\ \Downloads。转到IE Internet选项——>设置——>移动文件夹…

当使用内容配置附件和很长的文件名时,不能从IE打开文件

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.

IE团队需要修复这个bug,因为用户还在猜测该怎么做。对我来说,这是一个客户端问题。

#1


2  

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.

当完整路径超过251 chars时,Chrome足够聪明,可以截断文件名。在Windows系统上,Chrome保存到C:\用户 <用户名> \下载默认为支持更大的文件。

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.

IE 11的方法是跳过打开/保存点击,让取消您唯一有效的选择,而不告诉用户为什么。即防止长文件名,因为它节省了一个已经长路径C:\Users\ <用户名> \ AppData \当地\微软\ Windows \ \ Content.IE5 \ XXXXXXXX Internet临时文件。

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).

如果您控制了HTTP响应中的附件文件名,并且客户端是IE,那么您应该将附件文件名截断到最大字符号(考虑到默认的IE保存位置)。

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...

如果您无法控制服务器,那么您需要将IE中默认的保存位置更改为更短的路径,或者使用Chrome使用的——> C:\users\ \Downloads。转到IE Internet选项——>设置——>移动文件夹…

当使用内容配置附件和很长的文件名时,不能从IE打开文件

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.

IE团队需要修复这个bug,因为用户还在猜测该怎么做。对我来说,这是一个客户端问题。