C # example of downloading files and converting files into data streams

Source: Internet
Author: User

List the code first, and write the description later.

Protected void page_load (Object sender, eventargs e) <br/>{< br/> string file = request ["file"]; <br/> If (string. isnullorempty (File) <br/> return; <br/> string filepath = server. mappath ("~ /Download/"+ file); <br/> If (file. exists (filepath) <br/> // downloadstreamfile (filepath); <br/> toopen (filepath, ""); <br/> response. end (); <br/>}</P> <p> // <summary> <br/> // download the file <br/> /// </Summary> <br />/// <Param name = "serverfilpath"> </param> <br/> /// <Param name = "FILENAME"> </param> <br/> public static void todownload (string serverfilpath, string filename) <br/>{< br/> filestream = new filestream (serverfilpath, filemode. open); <br/> long filesize = filestream. length; <br/> httpcontext. current. response. contenttype = "application/octet-stream"; <br/> httpcontext. current. response. addheader ("content-disposition", "attachment; filename = \" "+ utf_filename (filename) + "\";"); <br/> /// attachment --- download as an attachment <br/> /// inline --- Open Online <br/> httpcontext. current. response. addheader ("Content-Length", filesize. tostring (); <br/> byte [] filebuffer = new byte [filesize]; <br/> filestream. read (filebuffer, 0, (INT) filesize); <br/> httpcontext. current. response. binarywrite (filebuffer); <br/> filestream. close (); <br/> httpcontext. current. response. end (); <br/>}</P> <p> // <summary> <br/> // open the file <br/> /// </Summary> <br />/// <Param name = "serverfilpath"> </param> <br/> /// <Param name = "FILENAME"> </param> <br/> public static void toopen (string serverfilpath, string filename) <br/>{< br/> filestream = new filestream (serverfilpath, filemode. open); <br/> long filesize = filestream. length; <br/> httpcontext. current. response. contenttype = "application/octet-stream"; <br/> httpcontext. current. response. addheader ("content-disposition", "inline; filename = \" "+ utf_filename (filename) +" \ ";"); <br/> httpcontext. current. response. addheader ("Content-Length", filesize. tostring (); <br/> byte [] filebuffer = new byte [filesize]; <br/> filestream. read (filebuffer, 0, (INT) filesize); <br/> httpcontext. current. response. binarywrite (filebuffer); <br/> filestream. close (); <br/> httpcontext. current. response. end (); <br/>}</P> <p> Private Static string utf_filename (string filename) <br/>{< br/> return httputility. urlencode (filename, system. text. encoding. utf8 ); <br/>}</P> <p> // <summary> <br/> // download the file data stream <br/> /// </Summary> <br/> // <Param name = "serverfilpath"> </param> <br/> Public static void downloadstreamfile (string serverfilpath) <br/>{< br/> int blocksize = 1024*100; <br/> byte [] buffer = new byte [blocksize]; <br/> byte [] fileheader = new byte [101]; </P> <p> int startindex = 0; <br/> int bytesread = 0; <br/> int bytesfinish = 0; <br/> int filesize = 0; <br/> string headertext = string. empty; <br/> binaryreader breader = NULL; </P> <p> breader = new binaryreader (file. openread (serverfilpath); <br/> filesize = convert. toint32 (breader. basestream. length); </P> <p> while (bytesfinish <filesize) <br/> {<br/> If (bytesfinish + blocksize <filesize) <br/> bytesread = breader. read (buffer, startindex, blocksize); <br/> else <br/> bytesread = breader. read (buffer, startindex, filesize-bytesfinish); </P> <p> bytesfinish + = bytesread; <br/> httpcontext. current. response. outputstream. write (buffer, 0, bytesread); <br/>}< br/> breader. close (); <br/> httpcontext. current. response. end (); <br/>}

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.