简体   繁体   English

上载没有FormData的文件

[英]Upload file without FormData

I am using XMLHttpRequest to upload large files from the browser directly to Amazon S3 like this (which works): 我正在使用XMLHttpRequest这样将大型文件从浏览器直接上传到Amazon S3(有效):

export const fileUploader = async (url, file) => {
  const xhr = new XMLHttpRequest()

  xhr.upload.addEventListener('load', () => {
    // ...
  })
  xhr.upload.addEventListener('error', () => {
    // ...
  })
  xhr.upload.addEventListener('abort', () => {
    // ...
  })
  xhr.upload.addEventListener('progress', () => {
    // ...
  })

  xhr.open('PUT', url)
  xhr.setRequestHeader('content-type', file.type)
  xhr.setRequestHeader('x_file_name', file.name)
  xhr.send(file)
}

For local development and testing I'd like to create a route in my node.js server which will accept files for upload like this. 对于本地开发和测试,我想在node.js服务器中创建一条路由,该路由将接受像这样的上传文件。

Server-side, the request.body is always empty: 服务器端, request.body始终为空:

router.put('/image-upload', koaBody(), async (ctx) => {
  console.log(ctx.request)

  // { method: 'PUT',
  // url: '/image-upload',
  // header:
  //  { host: 'localhost:3500',
  //    connection: 'keep-alive',
  //    'content-length': '324285',
  //    pragma: 'no-cache',
  //    'cache-control': 'no-cache',
  //    origin: 'http://localhost:3000',
  //    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.10 Safari/537.36',
  //    x_file_name: 'l1lnRw.jpg',
  //    'content-type': 'image/jpeg',
  //    accept: '*/*',
  //    referer: 'http://localhost:3000/gallery/4',
  //    'accept-encoding': 'gzip, deflate, br',
  //    'accept-language': 'en-US,en;q=0.9,fr;q=0.8' } }


  console.log(ctx.request.body) // {}
  console.log(ctx.req.body) // undefined
})

How do I go about uploading a file 'directly' to a Koa node.js server without wrapping it in FormData() ? 如何直接将文件“直接”上传到Koa node.js服务器而不将其包装在FormData() Thanks. 谢谢。

Here is how you upload a file without wrapping in FormData() in Koa: 这是在不包装Koa的FormData()情况下上传文件的方式:

import getRawBody from 'raw-body'

router.put('/image-upload', async (ctx) => {
  const file = await getRawBody(ctx.req)
  const bufferStream = new stream.PassThrough()
  const writeStream = fs.createWriteStream(`${config.staticDir}/file.jpg`)
  bufferStream.end(file)
  bufferStream.pipe(writeStream)

  ctx.body = {
    status: 'uploaded!'
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM