nodejs jquery download local file webstorm

1. Use the form submission method (ajax submission does not work)

$("#downloadTest").click(function() {
    $('#hidden_iframe').remove();
    var iframe = $('<iframe id="hidden_iframe" style="display:none;"></iframe>');
    var form = $('<form></form>')
        .attr('target','hidden_iframe')
        .attr('action','/service/downloadFile')
        .attr('method', 'get');
    var input = $('<input />').attr('name','fileName').attr('value', '@fileName').appendTo(form);
    iframe.append(form).appendTo(document.body);
    iframe.on('load', function() {
        window.reload();
    })
    $(form).submit();
})

2. Background (express finds the download method corresponding to the /service/downloadFile interface through routing )

var path = require('path');
exports.download = function(req, res) {
    var paths = path.join('D:/opt/files/event/28', 'tnsnames.ora');
    res.download(paths,function(err, data) {
        if(err) {
            console.log('download faied');
            res.sendStatus(404);
        }
    })
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325484256&siteId=291194637