我有以下用于上传到 Google Cloud 存储的快速端点。它工作得很好,来自 google api 的响应给了我一个唯一的文件名,我想将其传回我的前端:
app.post('/upload', (req, res) => {
var form = new formidable.IncomingForm(),
files = [],
fields = [];
form
.on('field', function(field, value) {
fields.push([field, value]);
})
.on('file', function(field, file) {
files.push([field, file]);
})
.on('end', function() {
console.log('-> upload done');
});
form.parse(req, function(err, fields, files){
var filePath = files.file.path;
bucket.upload(filePath, function(err, file, apiResponse){
if (!err){
res.writeHead(200, {'content-type': 'text/plain'});
res.end("Unique File Name:" + file.name);
}else{
res.writeHead(500);
res.end();
}
});
});
return;
});
我通过调用一个将文件传递给它的短函数来到达此端点:
function upload(file) {
var data = new FormData();
data.append('file', file);
return fetch(`upload`,{
method: 'POST',
body: data
});
}
const Client = { upload };
export default Client;
这个函数是从我的前端调用的,如下所示:
Client.upload(this.file).then((data) => {
console.log(data);
});
本次决赛console.log(data)
将响应记录到控制台。但是,我在任何地方都看不到我写的回复("Unique File Name:" + file.name
)
如何从客户端的响应正文中检索此信息?
The data
当我 console.log 时看起来像这样:
这是当我使用 Postman 将文件发布到端点时得到的响应: