Mvc4 webapi file download with jquery

Posted by Ray on Stack Overflow See other posts from Stack Overflow or by Ray
Published on 2012-11-28T05:02:31Z Indexed on 2012/11/28 5:03 UTC
Read the original article Hit count: 139

Filed under:
|
|

I want to download file on client side from 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.How can I popup a download with jquery(octet-stream) from api controller?

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

            });


});

but it was not work!!!Thanks!!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about mvc