Javascript 创建一个Axios post,使用Dropzone为Scala函数发送上传的文件,以处理相应的请求
我有下一个Scala函数:Javascript 创建一个Axios post,使用Dropzone为Scala函数发送上传的文件,以处理相应的请求,javascript,reactjs,scala,axios,react-dropzone,Javascript,Reactjs,Scala,Axios,React Dropzone,我有下一个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]]
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) => {.... })
SomeUploaded文件来自:
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')
);
var componentConfig={postrl:'no url'};
var djsConfig={autoProcessQueue:false}
var eventHandlers={addedfile:(someUploadedFile)=>…使用axios.post上载文件的代码…}
ReactDOM.render(
,
document.getElementById('content')
);
我的大问题是,我不知道如何在包含文件的情况下构建axios调用,以及如何添加与该文件相关的更多信息,以便Scala函数中的请求对象能够从json处理该文件。使用FormData发布文件,如下所示:
var formData = new FormData();
formData.append('file', someUploadedFile);
formData.append('otherAttribute', 'someValue');
axios.post('/upload', formData).then(...)