Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建文件以便在POST请求中动态下载_Javascript_Node.js_Express_Download - Fatal编程技术网

Javascript 创建文件以便在POST请求中动态下载

Javascript 创建文件以便在POST请求中动态下载,javascript,node.js,express,download,Javascript,Node.js,Express,Download,我试图弄清楚,是否可以创建一个文件而不将其存储在驱动器中,只是为了在POST请求中立即下载它 const specialRequests = async (req, res, next) => { // POST request ... // processing let xmlString = ... // get processed xml string // create xml file from that xmlString // initia

我试图弄清楚,是否可以创建一个文件而不将其存储在驱动器中,只是为了在POST请求中立即下载它

const specialRequests = async (req, res, next) => { // POST request
    ... // processing
    let xmlString = ... // get processed xml string

    // create xml file from that xmlString 
    // initialise download of the file, dont save it
    let file = ???
    res.download(file);
};
如果可能的话,下载方法被明确设计为以路径传输文件

您可以使用一些数据创建下载,但不能使用该方法

res.set('Content-Type', 'text/xml');
res.set('Content-Disposition', 'attachment; filename="example.xml"')
res.send(xmlString);

确实可以,但在客户端,文件下载不会启动,有没有办法通过设置一些额外的头来触发文件下载?@Mevia--文件下载会在我测试时启动。你的代码肯定还有其他问题。是的,所有工作正常,我使用fetch作为POST-request执行它,而当转换为GET-request时,它工作正常,谢谢。使用fetch部分是你的问题。您使用JavaScript显式地处理响应,而不是使用浏览器的默认处理,如果您导航到它,您将得到该处理。如果要在响应ajax请求时保存包含数据的文件,则需要编写JavaScript: