All download methods are summarized in ASP.

Source: Internet
Author: User
Tags urlencode

1         /// <summary>2         ///Download File TransmitFile3         /// </summary>4         /// <param name= "FilePath" ></param>5          Public Static voidDownloadFile (stringFilePath)6         {7             stringfilename =HttpContext.Current.Server.MapPath (filePath);8             varResponse =HttpContext.Current.Response;9Response. ContentType ="application/x-zip-compressed";TenResponse. AddHeader ("content-disposition","attachment;filename="+path.getfilename (filename)); One Response. TransmitFile (filename);  A         } -  -         /// <summary> the         ///Download Large Files -         /// </summary> -         /// <param name= "FilePath" ></param> -          Public Static voidDownloadbigfile (stringFilePath) +         { -FilePath = HttpContext.Current.Server.MapPath (FilePath);//Path +             stringFileName = Path.getfilename (FilePath);//file name saved by the client A             varResponse =HttpContext.Current.Response; atSystem.IO.FileInfo FileInfo =NewSystem.IO.FileInfo (filePath); -             if(Fileinfo.exists = =true) -             { -                 Const LongChunkSize =102400;//100K Read the file every time, only read 100K, this can alleviate the pressure of the server -  -                 byte[] buffer =New byte[ChunkSize]; in  - Response. Clear (); toSystem.IO.FileStream IStream =System.IO.File.OpenRead (filePath); +                 LongDatalengthtoread = Istream.length;//get the total size of the downloaded file -Response. ContentType ="Application/octet-stream"; theResponse. AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (FileName)); *                  while(Datalengthtoread >0&&Response. isclientconnected) $                 {Panax Notoginseng                     //Read size Response.OutputStream.Write (buffer, 0, lengthread); -                     intLengthread = istream.read (buffer,0, Convert.ToInt32 (ChunkSize)); the Response. Flush (); +Datalengthtoread = Datalengthtoread-Lengthread; A                 } the  + Response. Close (); -             } $         } $  -         /// <summary> -         ///Stream mode Download the         /// </summary> -         /// <param name= "FilePath" ></param>Wuyi          Public Static voidDownloadfilestream (stringFilePath) the         { -FilePath = HttpContext.Current.Server.MapPath (FilePath);//Path Wu             stringFileName = Path.getfilename (FilePath);//file name saved by the client -             varResponse =HttpContext.Current.Response; AboutFileStream fs =NewFileStream (FilePath, FileMode.Open); $  -             byte[] bytes =New byte[(int) fs. Length]; Fs. Read (Bytes,0, Bytes. Length); -  - FS. Close (); AResponse. ContentType ="Application/octet-stream"; +             //notifies the browser to download a file instead of opening it theResponse. AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (FileName, System.Text.Encoding.UTF8)); - Response. BinaryWrite (bytes); $ Response. Flush (); the Response. End (); the         } the  the         /// <summary> -         ///Download the text as a file in         /// </summary> the         /// <param name= "text" ></param> the         /// <param name= "FileName" ></param> About          Public Static voidDownloadtext (stringText,stringfileName) the         { the             byte[] bytes =Encoding.Default.GetBytes (text); the             varResponse =HttpContext.Current.Response; +Response. ContentType ="Application/octet-stream"; -             //notifies the browser to download a file instead of opening it theResponse. AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (FileName, System.Text.Encoding.UTF8));Bayi Response. BinaryWrite (bytes); the Response. Flush (); the Response. End (); -}

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.