How to upload a huge file in a gaming environment?

In my application, I am trying to upload a file to the destination folder, the browser does not allow me to upload a file larger than 4 GB, and my file size is 15 GB. I am amazed here and do not see the idea of ​​how to download it. Any help is very noticeable.

+4
source share
1 answer

You can solve this problem in two steps:

  • You need to break the huge file into smaller pieces, and then send them to the server. You can use some libraries for this javascript, for exampleResumable.js
  • In your play2 controller, you need to collect these pieces using the API Iteratee, and then you can do whatever you want with your file.

EDIT:

Resumable.js , ,

( ):

@()
@main("File upload"){
    <a href="#" id="browseButton">Select files</a>
}

javasctipt:

$(function(){
  var r = new Resumable({
    target:'/test/upload'
  });

  r.assignBrowse(document.getElementById('browseButton'));

  r.on('fileSuccess', function(file){
    console.debug(file);
  });
  r.on('fileProgress', function(file){
    console.debug(file);
  });
  // more events, look API docs
});

main.scala.html:

@(title: String)(content: Html)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/style.css")">
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
        <script src="@routes.Assets.at("javascripts/jquery-1.9.0.min.js")" type="text/javascript"></script>
        @*We include resumable.js library*@
        <script src="@routes.Assets.at("javascripts/resumable.js")" type="text/javascript"></script>
        @*Our javascript for file upload*@
        <script src="@routes.Assets.at("javascripts/upload.js")" type="text/javascript"></script>
    </head>
    <body>
        @content
    </body>
</html>

, :

, , chanks - Array [Byte] .

 // hadle file part as Array[Byte]
  def handleFilePartAsByteArray: PartHandler[FilePart[Array[Byte]]] =
    handleFilePart {
      case FileInfo(partName, filename, contentType) =>
        // simply write the data to the a ByteArrayOutputStream
        Iteratee.fold[Array[Byte], ByteArrayOutputStream](
          new ByteArrayOutputStream()) { (os, data) =>
          os.write(data)
          os
        }.mapDone { os =>
          os.close()
          os.toByteArray
        }

:

// custom body parser to handle file part as Array[Byte]
  def multipartFormDataAsBytes:BodyParser[MultipartFormData[Array[Byte]]] =
    multipartFormData(handleFilePartAsByteArray)

:

def handleFileUpload = Action(multipartFormDataAsBytes){ request =>
 // retrieve file name from data part   
 val fileName  = request.body.asFormUrlEncoded.get("resumableFilename").get.headOption
 // retrieve arrays of byte from file part and write them to file
    request.body.files foreach{
      case FilePart(key,filename,content,bytes)=>
        import scalax.io._
        val output:Output = Resource.fromFile(fileName.getOrElse("default"))
        output.write(bytes)
    }
    Ok("")
  }

:

import play.api.mvc._
import play.api.mvc.BodyParsers.parse.Multipart._
import play.api.libs.iteratee.Iteratee
import java.io.ByteArrayOutputStream
import play.api.mvc.BodyParsers.parse._
import play.api.mvc.BodyParsers.parse.Multipart.FileInfo
import play.api.mvc.MultipartFormData.FilePart
+18

All Articles