我有以下用于上传到谷歌云存储的快速端点.它工作得很好,来自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
)中所写的回复
如何从客户端的响应主体中检索此信息?
当我安慰时,data
是这样的.记录它:
这是我使用Postman将文件发布到我的端点时得到的响应: