![](/img/trans.png)
[英]How To Set a .json file as a variable for use in window.open (javascript)?
[英]How to handle json response & file both in window.open?
我正在使用window.open(来自React的jsx文件)下载文件:
function downloadFile(documentName, event) {
var response = window.open('/downloadViewDocument/'+ documentName, "_self");
}
从节点/表达式,我正在返回文件,如下所示:
var document = __dirname + "/../../attachments/" + req.params.documentName.trim();
fs.stat(document, function(err, stat) {
if(err == null) {
console.log('File exists');
res.setHeader("Content-Disposition","attachment; filename=\"" + req.params.documentName.trim() + "\"");
var filestream = fs.createReadStream(document);
filestream.pipe(res);
} else if(err.code == 'ENOENT') {
// file does not exist
**res.json({success: true, msg: 'File Does Not Exist!'});**
} else {
**res.json({success: true, msg: 'Error Occured, Please Try Again!'});**
}
});
请注意,在上面的代码中,如果出现错误,我将返回JSON对象。
如果找到文件,则将其下载。
但是,如果找不到文件,我想从味精中读取并希望向用户显示正确的错误消息。
我看不到window.open的任何选项来回读响应。 还有其他办法吗?
最简单的方法是只使用window.alert()
打印错误消息。
还有很多其他解决方案,但是如果我正确理解了您的问题,您只是想以一种简单的方式将错误消息打印给客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.