mercredi 4 novembre 2015

Saving a application/octet-stream to disk from javascript in IE9

I provide a method on my WebApi to download a file:

public HttpResponseMessage Get(int id)
    {
            MemoryStream file = RetrieveFile(id);
            if (file != null)
            {
                var response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(file);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "dummy.txt"
                };

                return response;
            }


        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }

This works fine and i can consume it with a WebClient.

I also tied to implement the javascript front-end using FileSaver.js :

$http({
            url: myUrl,
            method: 'GET',
            params: {
                id: 1
            },
            headers: {
                'Content-type': 'application/json'
            }
        }).success(function (data, status, headers, config) {
            var file = new Blob([data], {
                type: 'application/octet-stream'
            });
            saveAs(file, "test.txt");

        }).error(function (data, status, headers, config) {

        });

This works great but it fails on IE9 since FileSaver and Blop is not supported. I have tried opening the url in a new window but can't find a decent working solution for IE9

Aucun commentaire:

Enregistrer un commentaire