Issue while requesting a file(image/pdf/excel sheet) from server and then pipe to res

只谈情不闲聊 提交于 2019-12-14 04:08:38

问题


Scenario:

I have to send user file which is stored remotely in a server(cloudinary) to browser. I'm able to request an image from server and then pipe it to browser but it's not working for other files like pdf, excel sheet, etc.

exports.fetchFile = function(req, res, next){
var fileUrl = "/" + req.params.fileUrl;
console.log(fileUrl);

async.auto({
    'A': function(callback) {
        var queryObj = { 'myUrl' : fileUrl};
        // DB Query to retrieve user file information like url, type, etc.
        UserFileCtrl.fetchUserFile(queryObj, callback);
    }
}, function(err, results){
    if(err) {
        return res.send(500, "error");
    }

    var secureUrl = results['A']['secureUrl'];
    var fileType = results['A']['fileType'];
    // this will be "application/pdf" for pdf
    res.setHeader('Content-Type', fileType);

    request(secureUrl).pipe(res);
}); 
}

Above code gives an empty pdf file to client whereas if I open directly the cloudinary url then it's coming properly. I'm breaking my head for a long time but not able to get something out of it.

I really appreciate your help.

Edit: I found this similar question also: Express server send empty PDF file but this is also not working for me. I tried disabling connnect-livereload but even then it's not working.

The issue is happening only for pdfs. All other formats are working fine.

来源:https://stackoverflow.com/questions/37517312/issue-while-requesting-a-fileimage-pdf-excel-sheet-from-server-and-then-pipe-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!