【问题标题】:Not able to upload xlsx or csv file to azure blob storage无法将 xlsx 或 csv 文件上传到 azure blob 存储
【发布时间】:2020-07-13 00:58:27
【问题描述】:

想要将 XLSX 或 CSV 文件上传到 azure。所以我创建了一个 API

http://localhost:3000/upload

Post Request using post man

这是代码


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


    【解决方案1】:

    请尝试更改以下代码行:

    const uploadBlobResponse = await blockBlobClient.upload(files,files.file.size);
    

    const uploadBlobResponse = await blockBlobClient.uploadFile(files.file.path);
    

    【讨论】:

    • 不工作。还是同样的问题。请查看上面已编辑问题中console.log(files) 的输出
    • 对不起,我的错!更新了我的答案。请试一试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2019-12-01
    • 2017-01-24
    • 2017-08-19
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多