【问题标题】:Mvc4 webapi file download with jquery使用 jquery 下载 Mvc4 webapi 文件
【发布时间】:2012-11-28 15:34:47
【问题描述】:

我想从 api 下载客户端文件
api控制器:

     public HttpResponseMessage PostOfficeSupplies()
     {
        string csv = string.Format ("D:\\Others\\Images/file.png");
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StringContent(csv);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue     ("application/octet-stream");

        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = "file.png";
        return result;    
    }

1.如何从 api 控制器使用 jquery(octet-stream) 弹出下载?

     my client side code:
     $(document).ready(function () {
            $.ajax(
            {
                url: 'api/MyAPI'
            , type: "post"
            , contentType: "application/octet-stream"
            , data: ''
            , success:
            function (retData) {
                $("body").append("<iframe src='" + retData + "' style='display: none;' ></iframe>");
                               }

            });


});

但它不起作用!

【问题讨论】:

    标签: jquery asp.net-mvc asp.net-web-api


    【解决方案1】:

    更改以下代码

    result.Content = new StringContent(csv);
    

    到这里:

    result.Content = new StreamContent(File.OpenRead("D:\\Others\\Images\\file.png"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 2017-06-16
      • 2015-04-09
      • 2010-11-20
      • 1970-01-01
      相关资源
      最近更新 更多