0

I am downloading files in express js and react Js with ajax request. pdf , php , zip , etc every file is downloading but when i want to download docx file it is not working.

my code dowload every type of file but not docx file.

Front End Code

  getfile(cell) {



    console.log(cell)

    var filename = cell ;

    $.ajax({
      type: 'GET',
      url: '/filedownload/' + filename,
      success: function () {
        window.open('/filedownload/' + filename, '_blank');
      }
    });

  }

server side code

app.get('/filedownload/:filename', function (req, res) {

    var file = req.params.filename

    res.download(__dirname + '/uploads/' + file);

  });
Nahid Hasan
  • 687
  • 1
  • 10
  • 34

1 Answers1

0

It doesn't work that way. If you do an AJAX request the most likely thing you'll get is a file blob on success.

You can try using this to download files on the client after an ajax request.

This lib actually let's you download straight from an url like:

download("/robots.txt");

Using a fetch for AJAX request, I've done this:

// Using async/await ES6 javascript

const res = await fetch(fileDownloadUrl);

const fileBlob = await res.blob();

// using downloadjs https://www.npmjs.com/package/downloadjs
download(fileBlob);

In your case you must make sure you receive the blob

João Cunha
  • 9,929
  • 4
  • 40
  • 61