我正在尝试从“multipart / form-data”表单上传多个文件,并将它们全部存储在具有批次ID的文件夹中(这只是一个时间戳)
问题是我目前只能保存一个文件。
查看
@helper.form(action = routes.Application.upload, 'enctype -> "multipart/form-data", 'multiple -> "") {
<input type="file" name="fsa" multiple="">
<p>
<input type="submit">
</p>
}
控制器
def upload = Action(parse.multipartFormData) { request =>
request.body.file("fsa").map { fsa =>
import java.io.File
val filename = fsa.filename
val contentType = fsa.contentType
val timestamp: Long = System.currentTimeMillis / 1000
fsa.ref.moveTo(new File("/tmp/"+timestamp+"/"+filename))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.Application.index).flashing(
"error" -> "Missing file"
)
}
}
这与reqest.body.file只有一个文件有关,或者我应该迭代一个数组还是什么?不太熟悉scala,所以任何帮助表示赞赏。
答案 0 :(得分:3)
我花了一段时间才弄明白,可能会有更优雅的解决方案,但看到你已经等了6个月,我会告诉你我丑陋的解决方案:
在前端,我使用XHR将文件发送到服务器,将文件单独附加到表单:
var uploadFiles = document.getElementById("file-input").files;
var formData = new FormData();
for (var i = 0; i < uploadFiles.length; i++) {
console.log("appending " + uploadFiles[i].name);
formData.append(uploadFiles[i].name, uploadFiles[i]);
}
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("load", function(e) {
console.log("upload successful");
}, false);
xhr.upload.addEventListener("progress", function updateProgress(event) {
if (event.lengthComputable) {
var percentComplete = (event.loaded / event.total)*100;
console.log("Completed: " + percentComplete);
}
}, false);
xhr.open('POST', UPLOAD_URL, true);
xhr.send(formData);
服务器端代码:
object Api extends Controller {
def upload = Action(parse.multipartFormData) { request =>
println("Api.upload()")
LibraryService.uploadFiles(request.body.files)
Ok("Upload complete")
}
}
object LibraryService {
val libraryRoot: String = Play.configuration.getString("library.root").get;
def uploadFiles(files: Seq[FilePart[TemporaryFile]]) = {
files.foreach { filePart =>
val newFile = new File(libraryRoot + filePart.filename)
if (newFile.exists) {
println("The file " + newFile.getAbsolutePath + " already exists. Upload cancelled.")
} else {
filePart.ref.moveTo(newFile)
}
}
}
}
将文件上传为列表证明更具挑战性,我也只能获得对该列表中第一个文件的引用。