3

我正在尝试使用 scala 在播放框架中上传多个大文件。我仍然是一个 scala 和玩菜鸟。

从这里得到了一些很棒的代码,它让我完成了 90% 的工作,但现在我又被卡住了。

我现在遇到的主要问题是我只能读取文件数据,而不能读取任何其他已上传的数据,并且在浏览播放文档之后,我不清楚如何从这里获取。任何建议表示赞赏!

  def directUpload(projectId: String) = Secured(parse.multipartFormData(myFilePartHandler)) { implicit request =>
    Ok("Done");
  }


def myFilePartHandler: BodyParsers.parse.Multipart.PartHandler[MultipartFormData.FilePart[Result]] = {
    parse.Multipart.handleFilePart {
      case parse.Multipart.FileInfo(partName, filename, contentType) =>
        println("Handling Streaming Upload:" + filename + "/" + partName + ", " + contentType);

        //Set up the PipedOutputStream here, give the input stream to a worker thread
        val pos: PipedOutputStream = new PipedOutputStream();
        val pis: PipedInputStream = new PipedInputStream(pos);
        val worker: UploadFileWorker = new UploadFileWorker(pis,contentType.get);
        worker.start();

        //Read content to the POS
        play.api.libs.iteratee.Iteratee.fold[Array[Byte], PipedOutputStream](pos) { (os, data) =>
          os.write(data)
          os
        }.mapDone { os =>
          os.close()
          worker.join()
          if( worker.success )
            Ok("uplaod done. Size: " + worker.size )
          else
            Status(503)("Upload Failed");
        }
    }
  }
4

1 回答 1

0

您必须处理数据部分。正如您可以猜到(或在文档中查找)处理数据部分的函数称为:handleFilePart.

def myFilePartHandler: BodyParsers.parse.Multipart.PartHandler[MultipartFormData.FilePart[Result]] = {
   parse.Multipart.handleFilePart {
     // ...
   }
   parse.Multipart.handleFilePart {
     // ...
   }
}

另一种方法是handlePart方法。查看文档以获取更多详细信息。

于 2013-03-05T16:49:13.250 回答