javascript - Downloaded files in Angularjs doesn't saves properly -
my web api method either returns single file or zip file having multiple files. web api implementation.
public httpresponsemessage download([fromuri] list<string> filenames) { try { httpresponsemessage result = new httpresponsemessage(httpstatuscode.ok); if (filenames.count > 1) { list<string> filepaths = new list<string>(); memorystream memorystreamoffile = new memorystream(); using (zipfile zip = new zipfile()) { foreach (var filename in filenames) { zip.addfile(httpruntime.appdomainapppath + @"\uploaded\" + filename); } zip.save(memorystreamoffile); memorystreamoffile.seek(0, seekorigin.begin); result.content = new streamcontent(memorystreamoffile); result.content.headers.contentdisposition = new contentdispositionheadervalue("attachment"); result.content.headers.contentdisposition.filename = "files.zip"; result.content.headers.contenttype = new mediatypeheadervalue("application/octet-stream"); return result; } } if (!string.isnullorempty(filenames[0])) { string filepath = httpruntime.appdomainapppath + @"\uploaded\" + filenames[0]; var stream = new filestream(filepath, filemode.open, fileaccess.read); result.content = new streamcontent(stream); result.content.headers.contenttype = new mediatypeheadervalue("application/octet-stream"); return result; } return this.request.createresponse(httpstatuscode.notfound, "file not found."); } catch (exception ex) { return this.request.createresponse(httpstatuscode.internalservererror, ex); } }
this method working fine, i'm sure because when request method through "advanced rest client", google chrome extension, file gets downloaded , works perfectly.
but when request method through angularjs service call, file doesn't open. image files give me following error
following pdf
and following zip
from advance rest client behavior expected, not angularjs. following angular code
"download": { method: "get", url: "api/files/download/", params: { filenames: "@filenames" }, responsetype: "arraybuffer", headers: { 'content-type': "application/octet-stream" } }
return filemanagerclientservice.download({ filenames: filename }) .$promise .then(function (data) { console.log(data); appinfo.setinfo({ message: "files downloaded successfully" }); try { var blob = new blob([data]); if (typeof (filename) == "string") filesaver.saveas(blob, filename); else filesaver.saveas(blob, "allfiles.zip"); } catch (ex) { console.log(ex); } },
please tell me i'm missing on angular side? i've tried many code snippets didn't work!
so guys, issue resolved replacing $resource $http call. here working code,
function download(filename) { appinfo.setinfo({ busy: true, message: "loading files" }); return $http({ method: "get", url: "api/files/download/", params: { filenames: filename }, responsetype: "arraybuffer" }).then(function (data) { appinfo.setinfo({ message: "files downloaded successfully" }); try { var blob = new blob([data.data]); if (typeof (filename) == "string") filesaver.saveas(blob, filename); else filesaver.saveas(blob, "allfiles.zip"); } catch (ex) { console.log(ex); } }, function (error) { }); }
this working code, hope have better solution using $resource , why wasn't working $resource anyway.
Comments
Post a Comment