ASP. Problems and solutions for the download of long file names

Source: Internet
Author: User
Tags urlencode

In ASP. Write an attachment to upload and download the program, the attachment upload to the database, and then save the GUID of the attachment, we can find the database according to the GUID of the attachment, the general attachment download code is:

<!--<br/> <br/> Code highlighting produced by Actipro Codehighlighter (freeware) <br/> Http://www.C odehighlighter.com/<br/> <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 ();
} /span>

Here the more important is Response.appendheader ("content-disposition", "attachment; Filename=\ "" + httputility.urlencode (file. filename) + "\" "), where the Chinese file name needs to be encoded, by default the UTF8 encoding used. But the file name becomes very long after encoding, for example I now have a file called:

Project Inspection Registration Form (terminal)-empty. Xls

We do a network grab, and we can see that the HTTP response when downloading the file is:

http/1.1 OK
Cache-control:private
content-length:44032
Content-type:application/vnd.ms-excel
server: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%81%e6%a3%80%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, April 08:00:26 GMT

Can get encoded after the file name becomes:

%e6%8b%9b%e6%a0%87%e9%80%81%e6%a3%80%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 in the HTTP header, because of the browser or other reasons, for such a long HTTP header, the system will be the string cutoff, it will cause the download when the file name is not full or simply garbled situation. I tried, the download of this file in IE8 is completely normal, but in IE6 inside will cause the string cut-off, into "%a0%87 test prototype Project inspection registration form (terminal)-empty. XLS ". Different browsers have different deadlines.

There are 2 kinds of solutions, 1 is to limit the file name of users to upload files or when we download the code to the file name cutoff, to avoid garbled situation, but this caused the user experience is not good. That's a 2nd solution: Use the GB2312 encoding to output Chinese names directly using the UTF8 UrlEncode encoding.

The specific code is:

<!--<br/> <br/> Code highlighting produced by Actipro Codehighlighter (freeware) <br/> Http://www.C odehighlighter.com/<br/> <br/>--ProtectedvoidPage_Load (ObjectSENDER,EVENTARGSE)
{
Postlogiclogic=NewPostlogic ();
If(request.querystring["AID"]!=Null)
{
Response.Clear ();
Encodingcode=Encoding.GetEncoding ("gb2312");
Response.ContentEncoding=Code
Response.headerencoding=Code//That's a very important sentence.
Attachmentfile=Logic. Getattachmentbyid (NewGuid (request.querystring[ "aid" . ToString ()));
Response.AddHeader ( "content-type" ,file. Type);
Response.appendheader ( "content-disposition "," attachment;filename =\+file. Filename+\" // here do not encode work, direct output Chinese string
Response.BinaryWrite (file. Data.toarray ());
Response.End ();
}
} /span>

So the output is the long Chinese name. We'll grab the bag. Look at the header of the HTTP response:

http/1.1 OK
Cache-control:private
content-length:44032
Content-type:application/vnd.ms-excel
server:microsoft-iis/6.0
X-powered-by:asp.net
microsoftsharepointteamservices:12.0.0.6219
x-aspnet-version:2.0.50727
Content-disposition:attachment; Filename= "Tender test prototype Project inspection registration form (terminal)-empty. XLS"
date:wed, April 09:04:34 GMT

The problem is solved. Of course, if the user uploads a file that is a long, long filename, there is no way, and the database design field may not be that long. That's a good time to limit the upload.

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.