【发布时间】:2020-07-13 00:58:27
【问题描述】:
想要将 XLSX 或 CSV 文件上传到 azure。所以我创建了一个 API
http://localhost:3000/upload
这是代码
app.post('/upload',(req,res)=>{
var form = new formidable.IncomingForm();
form.parse(req, async(err,fields,files)=> {
const blobServiceClient = await BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
const containerClient = await blobServiceClient.getContainerClient('mycon');
if(!containerClient.exists()){
const createContainerResponse = await containerClient.create();
}
const blobName = files.file.name;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(files,files.file.size);
console.log(files);
res.send({message:files.file.size + "uploaded"})
})
})
当我在上面的代码 sn-p 中执行 console.log(files) 时
{
file: File {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
size: 18,
path: 'C:\\Users\\bakhil\\AppData\\Local\\Temp\\upload_8531745c7d9ae14f105d50c775256e00',
name: 'Book1.csv',
type: 'application/vnd.ms-excel',
hash: null,
lastModifiedDate: 2020-04-01T10:03:55.885Z,
_writeStream: WriteStream {
_writableState: [WritableState],
writable: false,
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
path: 'C:\\Users\\bakhil\\AppData\\Local\\Temp\\upload_8531745c7d9ae14f105d50c775256e00',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
autoClose: true,
pos: undefined,
bytesWritten: 18,
closed: false
}
}
}
当我从 Postman 触发端点时,遇到以下问题:
UnhandledPromiseRejectionWarning: Error: body must be a string, Blob, ArrayBuffer, ArrayBufferView, or a function returning NodeJS.ReadableStream.
我不知道这个错误。请帮我解决它。
提前致谢。
【问题讨论】:
标签: node.js azure-storage azure-blob-storage