In general, we can use HttpHandler to deal with some simple logic, such as verification code, download files and so on. To download the Word document as an example of how to download files in Hhtphandler, not limited to Word documents, if you download other files, you need to be aware of the "
Context. Response.ContentType = "Application/msword";
"Set to another appropriate format or common format"
Application/octet-stream"To see the code
1 //file name2 Const stringFileName ="application form. doc";3 //Get file path4 stringPath = context. Server.MapPath ("./app_data/") +FileName;5 //defining output MIME Types6Context. Response.ContentType ="Application/msword";7 //download files as a file stream8 using(FileStream fs =NewFileStream (path, FileMode.Open))9 {Ten //Create a byte[] byte array with a length of file size to download One byte[] bytes =New byte[(int) fs. Length]; A //reads a file stream into a byte[] byte array -Fs. Read (Bytes,0, Bytes. Length); - //notifies the browser to download a file instead of opening it theContext. Response.AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (FileName, System.Text.Encoding.UTF8)); - //writes a byte[] byte array to the browser - context. Response.BinaryWrite (bytes); -}
Download files using the generic handler HttpHandler