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/>}