【问题标题】:Multiple Upload Controller in Play Framework / ScalaPlay Framework / Scala中的多个上传控制器
【发布时间】:2013-10-15 13:55:04
【问题描述】:

我正在尝试从“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 不太熟悉,因此感谢您的帮助。

【问题讨论】:

    标签: scala playframework-2.0 multipartform-data


    【解决方案1】:

    这花了我一段时间才弄清楚,可能还有更优雅的解决方案,但鉴于您已经等了 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)
                }
            }
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2015-09-30
      • 2016-10-08
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 2015-06-22
      • 1970-01-01
      相关资源
      最近更新 更多