A
A
Alexander Buki2018-12-25 02:26:02
Node.js
Alexander Buki, 2018-12-25 02:26:02

How to properly transfer files from server to express via jquery(ajax) get requests?

I make a get request to the server and return the pdf file. At the same time, something incomprehensible comes to the browser, the download does not occur.

I heard somewhere that ajax requests do not return a file and you need to download it from a link.
Below is the request code and handler on the server.

$.get('/backend/exportVisitors', {eve: eve}, (data) => {
            console.log('data', data);
    });


server handler:
app.get("/backend/exportVisitors", urlencodedParser, function (request, res) { //export visitors
    if(!request.body) {
        res.send('Ошибка!');
        return res.sendStatus(400);
    }
    res.setHeader('Content-Type', 'application/pdf');
    res.setHeader('Content-Disposition', 'attachment; filename=Visitors.pdf');
    // file.pipe(res);
    res.download('register/temp/registrationSertificate.pdf', 'Visitors.pdf');
});


Server on express:
var express = require("express");
app.use('/', express.static('register'));

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

And this is what the browser outputs in the console:
Q5Wdhn0PrzrMIAXvtr1FyjxcZL226k5mOJ0Z-yKD_LDw4Rvjaa14O72cmeQctEqmGBdYCJ_cKvHuHhZ_2ocpIQ% 3D% 3D? Uid = 0 & filename = 2018-12-25_02-23-03.jpg & disposition = inline & hash = & limit = 0 & content_type = image% 2Fjpeg & tknv = v2 & size = 2048x2048

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question