前端代码:后台代码:public class Test{public string getFilePath(){return "http://192.168.0.1//upload//文件1.doc";}}
如果是这种 a标签直接给href指定文件地址,有些文件是直接打开而不是下载,例如:.txt,.pdf格式的;
所以我把后台返回字符串改成以流的形式返回;例子如下:
public ActionResult getFilePath(string a_id) { string path = _DocClient.IGetDocAnnexSevicReturnByReceive(a_id);//获取文件地址 System.Net.HttpWebRequest httpWebRequest = null; string filename = path.Substring(path.LastIndexOf("/") + 1, path.Length - path.LastIndexOf("/") - 1);//获取文件名 httpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(path); System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); System.IO.Stream sr = httpWebResponse.GetResponseStream(); return File(sr, "text/html", filename); }