• Asp.net mvc 下载文件


    前言

    最近有需求需要下载文件,可能是image的图片,也可能是pdf报告,也可能是微软的word或者excel文件。

    这里就整理了asp.net mvc 和asp.net webapi 下载的方法

    ASP.NET MVC 下载

    在mvc中,control的returnresult有FileResult,描述如下:

    System.Web.Mvc.FileResult
          System.Web.Mvc.FileContentResult
          System.Web.Mvc.FilePathResult
          System.Web.Mvc.FileStreamResult

     下载的时候,因为要根据文件来设置其MimiType,如果是NET 4.5则可能使用一下代码:

    MimeMapping.GetMimeMapping(fileName)

    准备完毕只好,一下就是controller下载的方法:

    public FileResult MyFile()
    {
        string root = Server.MapPath("~/App_Data");
        string fileName = "test.jpg";
        string filePath = Path.Combine(root, fileName);
        string s= MimeMapping.GetMimeMapping(fileName);
                
        return File(filePath, s, Path.GetFileName(filePath));
    }

    asp.net webapi 下载

    webapi的下载和mvc不太一样,webapi需要设置返回的header等一系列属性:

    [HttpGet]
    public HttpResponseMessage Get(string name)
    {
        var root = System.Web.HttpContext.Current.Server.MapPath("~/App_Data");
        string fileName = name;
        string path = Path.Combine(root, fileName);
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new FileStream(path, FileMode.Open);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = fileName;
        //result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        result.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(fileName));
        result.Content.Headers.ContentLength = stream.Length;
        return result;
    }

    总结

    以上就是下载的一些个人总结。参考资料如下:

    https://stackoverflow.com/questions/3604562/download-file-of-any-type-in-asp-net-mvc-using-fileresult

    https://stackoverflow.com/questions/26038856/how-to-return-a-file-filecontentresult-in-asp-net-webapi

  • 相关阅读:
    ORA-29516: Aurora assertion failure: Assertion failure at jol.c:11157
    ORA-03113: end-of-file on communication channel
    RMAN还原问题
    Linux Shell 小数比较
    fetch() without execute() [for Statement "SHOW VARIABLES LIKE 'wsrep_on'
    yum [Errno 12] Timeout on
    Virtual Box下配置Host-Only联网方式详解
    SPI协议详解
    心情
    南大西洋异常区
  • 原文地址:https://www.cnblogs.com/julyluo/p/6914677.html
Copyright © 2020-2023  润新知