如何在KOA中解析二进制数据(“多部分/表单数据”)?

时间:2018-12-06 21:56:16

标签: koa

如果我发送带有文本选项的POST查询,则一切正常:

Public Class Form1
Public xx As New List(Of Integer)
Public yy As New List(Of Integer)
Public up As Boolean = True
Public down As Boolean = False
Public lefty As Boolean = False
Public righty As Boolean = False
Public sizey As Integer = -1
Public tik As Integer = 0
Public neww As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    newpart()
    newpart()
    newpart()
End Sub

Public Sub square(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

    e.Graphics.Clear(Color.Black)
    For a = 0 To sizey
        e.Graphics.FillRectangle(Brushes.Aqua, xx(a), yy(a), 20, 20)
    Next

End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Right Then
        righty = True
        lefty = False
        up = False
        down = False
    ElseIf e.KeyCode = Keys.Left Then
        righty = False
        lefty = True
        up = False
        down = False
    ElseIf e.KeyCode = Keys.Up Then
        righty = False
        lefty = False
        up = True
        down = False
    ElseIf e.KeyCode = Keys.Down Then
        righty = False
        lefty = False
        up = False
        down = True
    End If
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e _
 As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    square(sender, e)
End Sub

Private Sub clock_Tick(sender As Object, e As EventArgs) Handles head.Tick
    If up = True Then
        yy(0) = yy(0) - 20
    ElseIf down = True Then
        yy(0) = yy(0) + 20
    ElseIf lefty = True Then
        xx(0) = xx(0) - 20
    ElseIf righty = True Then
        xx(0) = xx(0) + 20
    End If
    Me.Refresh()
    For b = 0 To sizey - 1
        If yy(b) - yy(b + 1) = 0 Then
            xx(b + 1) = xx(b + 1) + (xx(b) - xx(b + 1))
        ElseIf xx(b) - xx(b + 1) = 0 Then
            yy(b + 1) = yy(b + 1) + (yy(b) - yy(b + 1))

            If neww = True Then
                neww = False
                Exit For
            End If
        End If
    Next
End Sub

Sub newpart()
    xx.Add(100)
    yy.Add(100)
    sizey = sizey + 1
    neww = True
    Return
End Sub

End Class

在服务器端(KOA),我可以获取解析的a.m.query数据:

query from front-end:

const request = require("request")
const options = {
 method: 'POST',
 url: 'http://localhost:4000/user',
 headers:    form: { data: '12345' }
 }

但是如果我发送带有二进制数据(文件)的POST查询:

ctx.request.method: "POST"
ctx.request.originalUrl: "user"
ctx.request.body.data: "12345"

我不知道,如何在ctx.request中使用服务器字段(KOA)中的二进制数据(“ image.jpg)访问该数据的任何字段...

1 个答案:

答案 0 :(得分:0)

您可以为此使用busboyI wrote a gist for doing this,但我将在此处添加一些评论。

让我们创建一个帮助程序,以一种友好的方式解析文件。

// parse.js
import Busboy from 'busboy'

/**
 * Parses a single file from a Node request.
 *
 * @param  {http.IncommingRequest} req
 * @return {Promise<{ file: Stream, filename: string>}
 */
export default function parse (req) {
  return new Promise((resolve, reject) => {
    const busboy = new Busboy({
      headers: req.headers,
      limits: {
        files: 1 // allow only a single upload at a time.
      }
    })

    busboy.once('file', _onFile)
    busboy.once('error', _onError)
    req.pipe(busboy)

    function _cleanup () {
      busboy.removeListener('file', _onFile)
      busboy.removeListener('error', _onError)
    }

    function _onFile (fieldname, file, filename) {
      _cleanup()
      resolve({ file, filename })
    }

    function _onError (err) {
      _cleanup()
      reject(err)
    }
  })
}

现在我们需要使用它。假设您要上传到AWS S3。

import Koa from 'koa'
import parse from './busboy'
import AWS from 'aws-sdk'

const app = new Koa()

const s3 = new AWS.S3({ 
  params: { Bucket: 'myBucket' } 
})

// Assuming this is a route handler.
app.use(async (ctx) => {
  const { file, filename } = await parse(ctx.req)
  // `file` is a Stream. Pass this to S3, Azure Blob Storage or whatever you want.
  // `filename` is the file name specified by the client.
  const result = await s3.upload({
    Key: filename,
    Body: file
  }).promise()

  ctx.body = result
})

为简便起见,这是在客户端上使用axios上传文件的方式。

// `file` is a DOM File object.
function upload (file) {
  const data = new window.FormData()
  data.append('file', file, file.name)
  return axios.post('/upload', data)
}
相关问题