要上传XLSX或CSV文件到天蓝色。所以我创建了一个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"})
})
})
当我在上面的代码片段中执行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.
我不知道该错误。请帮我解决它。
谢谢。
答案 0 :(得分:0)
请尝试通过更改以下代码行:
const uploadBlobResponse = await blockBlobClient.upload(files,files.file.size);
到
const uploadBlobResponse = await blockBlobClient.uploadFile(files.file.path);