NullPointerException
NullPointerException

Reputation: 70

When downloading zip file from linux server with node and angular 5,zip file successfully downloads but EMPTY

In my services.service.ts file I am passing a name of a zip and calling the route

services.service.ts

downloadFile(name) {
  console.log(name);
  let newName = name+".zip";
  console.log(newName);
  return this.http
    .get(`/services/downloadFile/${name}`, {
      responseType: ResponseContentType.Blob })
    .pipe(map(res => {
      return {
        filename: newName,
        data: res.blob()
      };
    }))
    .subscribe(res => {
      //  console.log('start download:',res);
        var url = window.URL.createObjectURL(res.data);
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.setAttribute('style', 'display: none');
        a.href = url;
        a.download = res.filename;
        a.click();
        window.URL.revokeObjectURL(url);
        a.remove(); // remove the element
      }, error => {
        console.log('download error:', JSON.stringify(error));
      }, () => {
        console.log('Completed file download.')
      });
}

After i call this route from service.js the zip file gets downloaded to windows but with no data service.js

router.get('/downloadFile/:name', function (req, res) {
  let name = req.params.name;
  let newName = name+".zip";
  res.download(`./config/${newName}`, newName, function (err) {

    if (err) {
      console.log(err);
      console.log("ERROR APPEARED");
    } else {
      console.log('file uploaded :)');
    }
  });
});

This code executes with no errors (I will get "file uploaded :)" in a console) but the downloaded zip I wont be able to open or extract because it will not contain data (appropriate data)

Upvotes: 2

Views: 174

Answers (1)

iLuvLogix
iLuvLogix

Reputation: 6430

I've encountered a similar problem once - In case you use token-based auth you probably forgot to add these in your headers, try:

.get(`/services/downloadFile/${name}`, {headers: new Headers({
      'Content-Type': 'application/json', // INFO: Format set to JSON
      'authorization': yourAuth.token.etc // INFO: Attach token
    }),responseType: ResponseContentType.Blob })

Hope that helps to you solve your issue..

Upvotes: 2

Related Questions