C# 後端檔案下載
private void DownFile(string filePath, string fileName)
{
string sExtension = Path.GetExtension(filePath);
string sContentType = GetContentType(sExtension);
byte[] ary = null;
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
int len = System.Convert.ToInt32(fs.Length);
ary = br.ReadBytes(len);
Response.AddHeader("Content-Disposition", "inline; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
Response.ContentType = sContentType;
Response.ContentEncoding = Encoding.Unicode;
Response.AddHeader("Content-Length", ary.Length.ToString());
Response.BinaryWrite(ary);
fs.Close();
}
private string GetContentType(string sExtName)
{
string sTypeName = "";
switch (sExtName.Replace(".", "").Trim().ToLower())
{
case "ppt":
sTypeName = "application/mspowerpoint";
break;
case "doc":
sTypeName = "application/msword";
break;
case "docx":
sTypeName = "application/msword";
break;
case "xls":
sTypeName = "application/vnd.ms-excel";
break;
case "xlc":
sTypeName = "application/vnd.ms-excel";
break;
case "xll":
sTypeName = "application/vnd.ms-excel";
break;
case "xlm":
sTypeName = "application/vnd.ms-excel";
break;
case "xlw":
sTypeName = "application/vnd.ms-excel";
break;
case "pdf":
sTypeName = "application/pdf";
break;
case "zip":
sTypeName = "application/zip";
break;
case "jpg":
sTypeName = "image/jpeg";
break;
case "png":
sTypeName = "image/png";
break;
case "gif":
sTypeName = "image/gif";
break;
case "bmp":
sTypeName = "image/bmp";
break;
case "txt":
sTypeName = "text/plain";
break;
default:
sTypeName = "application/octet-stream";
break;
}
return sTypeName;
}
標籤: C#
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁