C# 下载文件到服务器,并把此文件转为 2进制 流

来源:互联网 发布:马龙 知乎 编辑:程序博客网 时间:2024/06/03 16:58

后台控制器代码


//下载传真文件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);




0 0