很多時候我們去做檔案上傳,做完上傳覺得檔案下載變得非常簡單,
但很多時候下載會出亂碼名稱等一系列問題,自己做了個下載,把各個類中的源碼放一起了
可能有點亂,各位包含,其實主要是為了我自己以後用的方便,放到這裡
下載方法:
string filePath = thesisInfo.ThesisUrl.ToString().Trim(); //擷取檔案名稱
string Path = "";
if (Convert.ToInt32(Request["TYPE"]) == 1)
...{ //返回伺服器上的實體路徑,UpLoadURL是擷取WEBCONFIG裡配置的一個路徑,是下面的那個靜態方法
Path = Server.MapPath(UpLoadFile.UpLoadURL + "ThesisFiles/") + filePath;
}
if (System.IO.File.Exists(Path)) //判斷路徑是否存在
...{
string temp = string.Format("attachment;filename={0}", Server.UrlEncode(filePath));
Response.ClearHeaders();
Response.AppendHeader("Content-disposition", temp);
Response.WriteFile(HttpUtility.UrlDecode(Path, System.Text.Encoding.UTF8));//解析檔案名稱防止亂碼
Response.End();
}
else
...{
Response.Write("該檔案不存在!");
}
public static String UpLoadURL
...{
get
...{ //擷取WEBCONFIG上配置的哪個路徑,可以自己寫
string stemp = ConfigurationManager.AppSettings[SystemConst.KEY_UpLoad];
if (stemp == null)
...{
return SystemConst.DEFAULT_UpLoad;
}
else
...{
return stemp;
}
}
}