码迷,mamicode.com
首页 > Web开发 > 详细

.net 根据文件地址下载文件

时间:2015-05-07 18:39:45      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

前端代码:
<a href="/Test/getFilePath">
后台代码:
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);
           
        }

 

.net 根据文件地址下载文件

标签:

原文地址:http://www.cnblogs.com/JD-XIAOMEI/p/4485484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!