1、后台数据库中保存的是文件的相对路径:/**/**/文件名.xls
2、前台JS
let path = escape(url);
let name = url.split('/');
var a = document.createElement('a');
a.download = name[name.length - 1];
a.href = '路径?path=' + path;
$("body").append(a);
a.click();
$(a).remove();
3、后台
public void DownLoadFile(string path)
{
string newpath = Server.UrlDecode(path);
string[] filepara = newpath.Split('/');
string fileName = filepara[filepara.Length - 1];//客户端保存的文件名
string filePath = Server.MapPath(newpath);
if (!string.IsNullOrEmpty(newpath))
{
try
{
HttpResponse response = System.Web.HttpContext.Current.Response;
FileInfo fi = new FileInfo(filePath);
if (fi.Exists)
{
response.Clear();
response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpContext.Current.Server.UrlEncode(fileName));
response.AddHeader("Content-Length", fi.Length.ToString());
response.ContentType = "application/octet-stream";
response.Filter.Close();
response.WriteFile(fi.FullName);
response.End();
}
else
{
response.Status = "404 File Not Found";
response.StatusCode = 404;
response.StatusDescription = "File Not Found";
response.Write("File Not Found");
response.End();
}
}
catch (Exception)
{
}
}
}