In my application i am trying to upload a file to the destination folder, the browser doesn't allow me to upload the file of size greater than 4 GB and my file size is 15 GB.I am struck here and have no idea how to upload it.Any help is highly appreciable.
You can solve this problem in two stage:
javascript
libraries, for example Resumable.js
Iteratee
API and then your can do what ever you want with your file. EDIT:
Let's use resumable.js
as example for the client side, I will not dive into the details, you can find a documentation and the examples here
Our view will be minimalist(only link to select a file or files) :
@()
@main("File upload"){
<a href="#" id="browseButton">Select files</a>
}
Our 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
});
Our 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>
And our server side, in play controller :
First we need to create a function that will treat our file part by consuming our chanks and producing the result - Array[Byte] in our example.
// 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
}
then we can define a custom body parser:
// custom body parser to handle file part as Array[Byte]
def multipartFormDataAsBytes:BodyParser[MultipartFormData[Array[Byte]]] =
multipartFormData(handleFilePartAsByteArray)
and in the end our controller may looks like:
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("")
}
List of imports for the controller :
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With