Download a zip file in AngularJS -



Download a zip file in AngularJS -

trying have zip file download in angularjs looked @ angularjs: download pdf file server , coded controller as:

rolloutservice.export(rollout.id, function(data, status, headers) { var headers = headers(); var filename = headers['download-filename'] || 'file'; var octetstreammime = 'application/octet-stream'; var contenttype = headers['content-type'] || octetstreammime; if (navigator.mssaveblob) { var blob = new blob([data], { type: contenttype }); navigator.mssaveblob(blob, filename); } else { var urlcreator = window.url || window.webkiturl || window.mozurl || window.msurl; if (urlcreator) { var link = document.createelement("a"); if ("download" in link) { var blob = new blob([data], { type: contenttype }); var url = urlcreator.createobjecturl(blob); link.setattribute("href", url); link.setattribute("download", filename); var event = document.createevent('mouseevents'); event.initmouseevent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); link.dispatchevent(event); } else { var blob = new blob([data], { type: octetstreammime }); var url = urlcreator.createobjecturl(blob); window.location = url; } } } });

but file comes partly only.

trying unzip gives:

stephane@stephane-thinkpad-x60:~> unzip -l file archive: file error [file]: missing 96319383 bytes in zipfile (attempting process anyway) error [file]: start of central directory not found; zipfile corrupt.

note if skipping controller , going direct window.open() file comes finish , can unzipped.

on controller export request, browser console shows next headers:

remote address:127.0.0.1:8080 request url:http://localhost:8080/nitro-project-rest/rollouts/2/export request method:get status code:200 ok request headersview source accept:application/json, text/plain, */* accept-encoding:gzip,deflate,sdch accept-language:en authorization:basic bnnuqg5zbi5jb206zxrvawxl connection:keep-alive host:localhost:8080 origin:http://localhost:9000 referer:http://localhost:9000/ user-agent:mozilla/5.0 (x11; linux i686) applewebkit/537.36 (khtml, gecko) ubuntu chromium/34.0.1847.116 chrome/34.0.1847.116 safari/537.36 response headers access-control-allow-headers:accept-language,content-type,x-requested-with,accept,origin,access-control-request-method,access-control-request-headers,authorization access-control-allow-methods:post, put, get, options, delete access-control-allow-origin:http://localhost:9000 access-control-max-age:3600 cache-control:no-store cache-control:no-cache content-disposition:attachment; filename="orange-rollout-rollout-export.zip" content-length:1960 content-type:application/zip date:wed, 05 nov 2014 20:33:31 gmt download-filename:orange-rollout-rollout-export.zip expires:thu, 01 jan 1970 00:00:00 gmt pragma:no-cache server:apache-coyote/1.1 x-content-type-options:nosniff x-frame-options:deny x-xss-protection:1; mode=block

my $http service corrupting file when sending request.

i needed add together next configuration:

responsetype: 'arraybuffer'

as in:

factory.rolloutexport = function(rolloutid, successcallback, failurecallback) { $http({ url: env.nitro_project_rest_url + '/rollouts/' + rolloutid + '/export', method: 'get', responsetype: 'arraybuffer', cache: false, headers: { 'content-type': 'application/json; charset=utf-8', 'authorization': authservice.getcredentialsheaders() } }).success(successcallback).error(failurecallback); };

now zip file comes untouched encoding converter.

angularjs download

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -