[英]Download or view file using google firebase cloud function
What I'm trying to do: 我正在尝试做的是:
My code 我的密码
let createPdf = functions.https.onRequest(async (request, response) => {
// more code here
if (download == 'true') {
return response.status(200).download(__dirname + "/docs/" + newFileName, newFileName, (err) => {
if (err) {
console.log(err.message);
} else {
console.log("Downloaded:", filename)
}
})
} else {
var options = {
root: __dirname,
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
return response.status(200).sendFile("/docs/" + newFileName, options, (err) => {
if (err) {
console.log(err);
} else {
console.log('Sent:', filename);
}
});
}
});
The error 错误
{
"error": {
"code": 500,
"status": "INTERNAL",
"message": "function crashed",
"errors": [
"socket hang up"
]
}
}
Note: When I return a simple string instead of the file it works. 注意:当我返回一个简单的字符串而不是文件时,它将起作用。
Use express request and response objects to send a file. 使用快速请求和响应对象发送文件。
Read this documentation: using_express_request_and_response_objects 阅读此文档: using_express_request_and_response_objects
And don't forget to use absolute path when using sendFile 并且在使用sendFile时不要忘记使用绝对路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.