1

我正在尝试从“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不太熟悉,所以任何帮助表示赞赏。

4

1 回答 1

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)
            }
        }
    }
}

事实证明,将文件作为列表上传更具挑战性,我也只能获得对该列表中第一个文件的引用。

于 2014-04-27T09:58:14.050 回答