asp.net文件下載
switch (Convert.ToInt32(Request["type"]))
{
//流方式下載 方法一
case 1:
string filename = "123.pdf";
string filepath = Server.MapPath("upload/123.pdf");
FileStream fs = new FileStream(filepath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "p_w_upload; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.Default));
Response.BinaryWrite(bytes);
Response.Flush();
break;
case 2://方法2
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "p_w_upload;filename=z.zip");
string filename2 = Server.MapPath("upload/aaa.zip");
Response.TransmitFile(filename2);
break;
case 3://方法三
string fileName = "123.pdf";
string filePath = Server.MapPath("upload/123.pdf");
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "p_w_upload;filename=" + fileName);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
break;
case 4://分塊下載,方法四
string fileName4 = "123.pdf";
string filePath5 = Server.MapPath("upload/123.pdf");
System.IO.FileInfo fileInfo4 = new System.IO.FileInfo(filePath5);
if (fileInfo4.Exists == true)
{
const long ChunkSize = 1024000;//每次讀取100K
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath5);
long dataLengthToRead = iStream.Length;//獲取文件長度
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "p_w_upload; filename=" + HttpUtility.UrlEncode(fileName4));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
break;
default:
Response.Write("ddd");
break;
}
Response.End();
下載文件方法封裝,怎樣根據文件的擴展名下載文件(如沒有擴展名,文件打開有可能出現亂碼)
void DownloadFile(string filename)
{
string saveFileName = "123.xls";
int intStart = filename.LastIndexOf("\\") + 1;
saveFileName = filename.Substring(intStart, filename.Length - intStart);
System.IO.FileInfo fi = new System.IO.FileInfo(filename);
string fileextname = fi.Extension;
string DEFAULT_CONTENT_TYPE = "application/unknown";
RegistryKey regkey, fileextkey;
string filecontenttype;
try
{
regkey = Registry.ClassesRoot;
fileextkey = regkey.OpenSubKey(fileextname);
filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
}
catch
{
filecontenttype = DEFAULT_CONTENT_TYPE;
}
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = true;
this.EnableViewState = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "p_w_upload;filename=" + saveFileName);
Response.ContentType = filecontenttype;
Response.WriteFile(filename);
Response.Flush();
Response.Close();
Response.End();
}