Asp.net利用一般处理程序实现文件下载功能
YPE html>
为äºé¡¹ç®å¨web表åæ件ä¸æ¹ä¾¿ç使ç¨æ件ä¸è½½çççåè½ï¼æ们å¨æä½çæ¶åä¸è½åå¤çä¸è¬ç¨åºè¿æ ·æä½ï¼ä»å¤©ä¸ºå¤§å®¶å¸¦æ¥äºAsp.netå©ç¨ä¸è¬å¤çç¨åºå®ç°æ件ä¸è½½åè½ï¼ä¸èµ·ççç±ç«ææ¯é¢éçä»ç»å§ï¼
é¦å æä¸ä¸ªhtml页é¢ï¼é¡µé¢æä¸ä¸ªé¾æ¥ï¼ç¹å»é¾æ¥å¼¹åºæ件ä¸è½½/ä¿å(ç±»ä¼¼è¿ é·ä¸è½½é¾æ¥)
æ件ä¸è½½ ä¸è½½readme.txtæ件
ä¸è½½readme.txtæ件
ä¸è¬å¤çç¨åºç代ç å¦ä¸
using System.IO; using System.Web; namespace Zhong.Web { ////// DownloadFileHandler çæè¦è¯´æ /// public class DownloadFileHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { string filePath = context.Server.MapPath("~/App_Data/readme.txt"); FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Dispose(); context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("Content-Disposition", "attachment; filename=readme.txt"); context.Response.BinaryWrite(bytes); context.Response.Flush(); //大æ件ä¸è½½ç解å³æ¹æ¡ //context.Response.ContentType = "application/x-zip-compressed"; //context.Response.AddHeader("Content-Disposition", "attachment;filename=z.zip"); //string filename = Server.MapPath("~/App_Data/move.zip"); //context.Response.TransmitFile(filename); } public bool IsReusable { get { return false; } } } }
ç¹å»ç¬¬ä¸ä¸ªé¾æ¥è®¿é®ï¼æ¾ç¤ºå¦ä¸ï¼
ç¹å»ç¬¬äºä¸ªé¾æ¥è®¿é®ï¼ä¸è½½æ件ï¼
ç±äºæä¹åå·²ç»æµè¯è¿ä¸æ¬¡ï¼æ以è¿æ¬¡ä¸è½½æ¶å½å为readme(1).txt
ä¸è¿°æ¯ç±ç«ææ¯é¢éå°ç¼ä»ç»çAsp.netå©ç¨ä¸è¬å¤çç¨åºå®ç°æ件ä¸è½½åè½ï¼å¤§å®¶é½å¦ä¼äºå?å¦ä¼äºçæå们就赶紧ç¨èµ·æ¥å§ï¼è§å¾æ们ä¸éççè¯æjs.aizhan.comæ¨èç»èº«è¾¹çæåå¦ã