环境:net framework 4.5.2
/// <summary>
/// 文件别名下载
/// </summary>
/// <param name="filePath">/File/NeedsDeliver/20211222/8c6f2254-c605-4d92-b916-7bebac6ae162.zip</param>
/// <param name="name">xxx.zip</param>
[HttpGet]
[Route("FileDown/")]
public void FileDown(string filePath, string name) {
HttpResponse response = HttpContext.Current.Response;
if (!string.IsNullOrEmpty(filePath)) {
filePath = HttpContext.Current.Server.MapPath(filePath);
response.ClearHeaders();
response.Clear();
response.Expires = 0;
response.Buffer = true;
response.AddHeader("Accept-Language", "zh-tw");
//string name = System.IO.Path.GetFileName(filePath);
using (System.IO.FileStream files = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) {
byte[] byteFile = new byte[1];
if (files.Length > 0) {
byteFile = new byte[files.Length];
}
files.Read(byteFile, 0, byteFile.Length);
files.Close();
response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
response.ContentType = "application/octet-stream;charset=gbk";
response.BinaryWrite(byteFile);
response.End();
}
}
}
window.open('当前接口地址')