0

我想从 api
apicontroller 下载客户端文件:

     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>");
                               }

            });


});

但这行不通!

4

1 回答 1

0

更改以下代码

result.Content = new StringContent(csv);

对此:

result.Content = new StreamContent(File.OpenRead("D:\\Others\\Images\\file.png"));
于 2012-11-28T07:34:47.813 回答