ASP. net file Download (txt,rar,pdf,word,excel,ppt)

Source: Internet
Author: User

aspx file download it is not difficult to say, but in the process of doing still encountered some small problems, because these small problems, resulting in the solution is too difficult, one of which is response.end (), causing the download file thread termination of the situation ...

The correct way to download the file

1              //get the contents of the corresponding file, the main comm. FileURL file Save dynamic Path, i.e. 20150825/5e7af276b7754363a1e78b496e1d1603 text document. txt2Commnoticemodel comm = Commnoticebll.getinstance (). Getcommnoticemodel (int. Parse (request.querystring["ID"]));3             //here the main composition of the relative path of the file, here is the ~/filebox/20150825/5e7af276b7754363a1e78b496e1d1603 text document. txt4             stringPath = Commonutilmodel.getfilevirtualpath () +Comm. FileURL;5             Try6             {7                               8FileInfo FileInfo =NewFileInfo (Server.MapPath (path));9 response.clear ();Ten response.clearcontent (); One response.clearheaders (); AResponse.AddHeader ("content-disposition","attachment;filename="+fileinfo.name); -Response.AddHeader ("Content-length", fileInfo.Length.ToString ()); -Response.AddHeader ("content-transfer-encoding","binary"); the                 //tell the browser to pass to the user is a non-txt,rar, etc. does not appear on the Ieem of a file, do not need to open in the browser page, need to download directly -Response.ContentType ="Application/octet-stream"; -response.contentencoding = System.Text.Encoding.GetEncoding ("gb2312"); - Response.WriteFile (fileinfo.fullname); + Response.Flush (); - Response.End ();  +             } A  at             Catch(Exception ex) -             { - response.clear (); - response.clearcontent (); - response.clearheaders (); -Response.Write (ex. Message +"<br/>"+path); in Response.End (); -}

Reprint Please specify source: http://www.cnblogs.com/abc1069/

ASP. net file Download (txt,rar,pdf,word,excel,ppt)

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.