后台控制器代码
//下载传真文件
public FileResult DownLoadFax()
{
string url = ToolMethor.GetUrlParam("faxDownLownUrl") + ToolMethor.GetUrlParam("faxName");//下载url
string fileName = url.Substring(url.LastIndexOf('/'));//文件名
string fileSuffix = url.Substring(url.LastIndexOf('.'));//文件后缀
string saveFolderPath = Request.PhysicalApplicationPath + @"Faxs/";//保存在哪个文件夹
if (!Directory.Exists(saveFolderPath))
{
Directory.CreateDirectory(saveFolderPath);//创建文件夹
}
WebClient wc = new WebClient();
wc.DownloadFile(url, saveFolderPath + fileName);//保存到本地
//读取本地文件变为 2进制 流
FileStream fs = new FileStream(saveFolderPath + fileName, FileMode.Open, FileAccess.Read);
byte[] by = new byte[fs.Length];
fs.Read(by, 0, (int)fs.Length);
fs.Close();
return new FileContentResult(by, fileSuffix);
}
其中 $.GetRootPath() 是我获取网站根目录的一个函数,比较简单的
window.open($.GetRootPath()
+ "/Fax/DownLoadFax"
+ "?faxDownLownUrl=" + encodeURIComponent("http://192.168.2.7:8091/") + "&faxId=" + faxId + "&faxName=" + faxName);