.NET 在浏览器中下载TXT文件

 通常我们用浏览器打开Txt文件时候,浏览器会直接打开,我们想要txt下载到本地该怎么操作,用js也可以,单不能兼容所有的浏览器,所以我们可以在服务端做处理,代码如下:

        //TXT文件生成页面
        public ActionResult FileDownLoad(string filepth)
        {
            string FileCompath= Server.MapPath(filepth);
            string Result = "";
            StreamReader strmer = new StreamReader(FileCompath, Encoding.Default);             
            string  linetext;
            while ((linetext = strmer.ReadLine()) != null)
            {
                Result += linetext+"\r\n";
            }
            strmer.Close();
            Response.Headers["Content-Disposition"] = "attachment;filename=aaa.txt";//输出文件格式
            Response.Charset = "utf-8";//防止乱码
            Response.Write(Result);
            return View();
        }

文件打开,记得关闭以释放资源;

 

 public ActionResult FileDownLoad(string filepth)
        {
            string FileCompath= Server.MapPath(filepth);
       System.IO.FileStream fs = null;
            fs = System.IO.File.Open(FileCompath, System.IO.FileMode.Open);
            byte[] btFile = new byte[fs.Length];
            fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
            fs.Close();

            Response.Headers["Content-Disposition"] = "attachment;filename=aaa.pdf";//输出文件格式
            Response.ContentType = "application/octet-stream";
            Response.BinaryWrite(btFile);
       
       return View();
   }

 

转载于:https://www.cnblogs.com/xibei666/p/5559148.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 ASP.NET Web API 实现文件下载的教程: 1. 创建 Web API 项目 首先,你需要创建一个 ASP.NET Web API 项目。在 Visual Studio 选择 File -> New -> Project,然后选择 ASP.NET Web Application,选择 Web API 项目模板并命名项目。 2. 添加文件下载方法 在 Web API 项目,你需要添加一个方法来处理文件下载请求。你可以在控制器类添加以下方法: ```csharp public HttpResponseMessage GetFile(string fileName) { var filePath = HttpContext.Current.Server.MapPath("~/App_Data/" + fileName); if (!File.Exists(filePath)) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, "File not found."); } var fileBytes = File.ReadAllBytes(filePath); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(fileBytes) }; response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentLength = fileBytes.LongLength; return response; } ``` 在上面的方法,我们首先检查请求的文件是否存在,如果不存在,则返回“File not found”的错误响应。如果文件存在,则读取文件的字节数组,并将其作为响应的内容。我们还设置了响应头的 Content-Disposition 和 Content-Type,以指示浏览器文件下载到本地计算机。 3. 添加路由 接下来,你需要为文件下载方法添加路由。在 WebApiConfig 类添加以下代码: ```csharp config.Routes.MapHttpRoute( name: "DownloadFile", routeTemplate: "api/files/{fileName}", defaults: new { controller = "files", action = "GetFile" } ); ``` 此路由将匹配形如“api/files/{fileName}”的 URL,并将其路由到 GetFile 方法。 4. 测试文件下载 现在,你可以启动 Web API 项目,并使用类似以下的 URL 来测试文件下载: ``` http://localhost:port/api/files/your_file_name.txt ``` 此 URL 将下载位于 App_Data 文件的“your_file_name.txt文件。 就是这样,你现在已经知道了如何使用 ASP.NET Web API 实现文件下载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值