在 header 加入一些東西,讓網頁辨識成檔案,進而去下載檔案。
//目錄下載圖片
string directory = "D:\\test\\";
string fileName = "xxx.jpg";
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.TransmitFile(directory + fileName);
Response.End();
一般網頁
下載網頁
ContentType
.htm, .html Response.ContentType = "text/HTML";
.txt Response.ContentType = "text/plain";
.doc, .rtf, .docx Response.ContentType = "Application/msword";
.xls, .xlsx Response.ContentType = "Application/x-msexcel";
.jpg, .jpeg Response.ContentType = "image/jpeg";
.gif Response.ContentType = "image/GIF";
.pdf Response.ContentType = "application/pdf";
資料來源:
http://www.c-sharpcorner.com/uploadfile/afenster/how-to-download-a-file-in-Asp-Net/
https://developer.mozilla.org/zh-TW/docs/Web/HTTP/Headers/Content-Disposition
完整教學
https://weblog.west-wind.com/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET
讀URL檔案
http://blog.darkthread.net/post-2008-10-14-download-file-with-c.aspx
Jquery dialog 方式去開啟圖片
http://jsfiddle.net/KyleMit/82QYd/