Problems related to Asp. Net long file name download and solutions

Source: Internet
Author: User
Tags urlencode microsoft iis

In Asp. net writes a program for uploading and downloading attachments, uploads the attachments to the database, and saves the GUID of the attachments. We can find the attachments in the database according to the GUID, the code for downloading attachments is:

<! -- <Br/> Code highlighting produced by Actipro CodeHighlighter (freeware) <br/> http://www.CodeHighlighter.com/<br/> --> privatevoidDownload (stringID)
{
File = logic. GetAttachmentByID (newGuid (ID ));
Response. AddHeader ("Content-Type", file. Type );
Response. AppendHeader ("Content-Disposition", "attachment; filename = \" "+ HttpUtility. UrlEncode (file. FileName) + "\"");
Response. BinaryWrite (file. Data. ToArray ());
Response. End ();
}

Response. appendHeader ("Content-Disposition", "attachment; filename = \" "+ HttpUtility. urlEncode (file. fileName) + "\" "); encode the Chinese file name. The default format is UTF8. However, after encoding, the file name will become very long. For example, I have a file named:

Bidding inspection prototype project inspection Registration Form (terminal)-empty. XLS

When we capture packets through the network, we can see that the HTTP response during file download is:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 44032
Content-Type: application/vnd. ms-excel
Server: Microsoft-Microsoft IIS/6.0
X-Powered-By: ASP. NET
MicrosoftSharePointTeamServices: 12.0.0.6219
X-AspNet-Version: 2.0.50727
Content-Disposition: attachment; filename = "% e6 % 8b % 9b % e6 % a0 % 87% e9 % 80% e6 % a3 % 81% e6 % a0 % b7 % e6 % 9c % ba % e9 % a1 % b9 % e7 % 9b % AE % e6 % a3 % 80% e6 % 9f % a5 % e7 % 99% bb % e8 % AE % b0 % e8 % a1 % a8 (% e7 % bb % 88% e7 % AB % af) -% e7 % a9 % ba. XLS"
Date: Wed, 25 Mar 2009 08:00:26 GMT

The encoded file name is changed:

% E6 % 8b % 9b % e6 % a0 % 87% e9 % 80% e6 % a3 % 81% e6 % a0 % b7 % e6 % 9c % ba % e9 % a1 % b9 % e7 % 9b % AE % e6 % a3 % 80% e6 % 9f % a5 % e7 % 99% bb % e8 % AE % b0 % e8 % a1 % a8 (% e7 % bb % 88% e7 % AB % af) -% e7 % a9 % ba. XLS

This is all in the HTTP header. For browser or other reasons, for such a long HTTP header, the system will end the string, this may cause incomplete file names or garbled characters during download. I tried it. the download of this file in IE8 is completely normal, but in IE6 it will cause the string to end, changed to "% a0 % 87 prototype inspection Registration Form (terminal)-null. XLS ". Different browsers have different ends.

There are two solutions: one is to restrict the file name uploaded by the user or we can write code to end the file name when downloading to avoid garbled characters, however, this results in poor user experience. There are 2nd solutions: Without UTF8 UrlEncode encoding, the Chinese name is output using gb2312 encoding.

The specific code is:

<! -- <Br/> Code highlighting produced by Actipro CodeHighlighter (freeware) <br/> http://www.CodeHighlighter.com/<br/> --> protectedvoidPage_Load (objectsender, EventArgse)
{
PostLogiclogic = newPostLogic ();
If (Request. QueryString ["AID"]! = Null)
{
Response. Clear ();
Encodingcode = Encoding. GetEncoding ("gb2312 ");
Response. ContentEncoding = code;
Response. HeaderEncoding = code; // This sentence is very important.
Attachmentfile = logic. GetAttachmentByID (newGuid (Request. QueryString ["AID"]. ToString ()));
Response. AddHeader ("Content-Type", file. Type );
Response. appendHeader ("Content-Disposition", "attachment; filename = \" "+ file. fileName + "\" "); // No encoding is performed here, and a Chinese string is output directly.
Response. BinaryWrite (file. Data. ToArray ());
Response. End ();
}
}

The output is a long Chinese name. Let's capture the packet to see the HTTP Response Header:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 44032
Content-Type: application/vnd. ms-excel
Server: Microsoft-Microsoft IIS/6.0
X-Powered-By: ASP. NET
MicrosoftSharePointTeamServices: 12.0.0.6219
X-AspNet-Version: 2.0.50727
Content-Disposition: attachment; filename = "Registration Form (terminal)-empty. XLS"
Date: Wed, 25 Mar 2009 09:04:34 GMT

The problem is solved in this way. Of course, if the user uploads a file with a long and long file name, there is no way, and the fields designed in the database may not be that long. It is necessary to make restrictions during the upload.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.