创建一个 Axios post 以使用 Dropzone 发送上传的文件,以便 Scala 函数处理相应的请求

Create an Axios post to send an uploaded file using Dropzone for a Scala function to handle the respective request

我有下一个 Scala 函数:

  def upload = Action(parse.multipartFormData)  { implicit request =>
//grabbing the values from the request
println("Request To Upload File = " + request.toString)
val values = request.body.dataParts
val category:Option[Seq[String]] = values.get("category")
val id:Option[Seq[String]] = values.get("id")


//Grabbing the parts of the file, and adding that into the logic
request.body.file("file").map { file =>
  val fileBytes = FileUtils.readFileToByteArray(new File(file.ref.file.getPath))
  val fileType = file.contentType.getOrElse("None")
  val encodedFile:String = Base64.getEncoder().encodeToString(fileBytes)
  val rowId = getElement(id)
  println(rowId)
  val record:FileRecord = FileRecord(rowId, getElement(userid), file.filename, getElement(category),getElement(project), "1",fileType,encodedFile,0)
  FileRecords.add(record)
  Ok(rowId)
}.getOrElse {
  BadRequest("Dragons won.")
}

}

我想创建一个将使用此功能的 axios post。类似于:

           axios.post('/upload', {id: someId, 
                                  category: someCategory,
                                  file: someUploadedFile
                                  })
                                .then((response) => {.... })

someUploadedFile 来自:

    var componentConfig = { postUrl: 'no-url' };
var djsConfig = { autoProcessQueue: false }

var eventHandlers = { addedfile: (someUploadedFile) => ... code to upload file with axios.post ..... }

ReactDOM.render(
    <DropzoneComponent config={componentConfig}
                       eventHandlers={eventHandlers}
                       djsConfig={djsConfig} />,
    document.getElementById('content')
);

我的大问题是,既然包含了一个文件,我不明白如何构建该 axios 调用,也不知道如何添加与该文件相关的更多信息,以便 Scala 函数中的请求对象能够处理该文件来自 json.

使用 FormData 来 post 一个文件,像这样:

var formData = new FormData();
formData.append('file', someUploadedFile);
formData.append('otherAttribute', 'someValue');

axios.post('/upload', formData).then(...)