Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload in playframework with different browsers

I'm using playframework to build a web site. And I also use a rich editor named xheditor.

Xheditor support ajax-fileuploading, it needs the server side has a action which accepts "filedata" parameter which contains the upload file.

So I wrote a upload action:

public class Application extends Controller {
    public static void upload(File filedata) { 
        // the filedata should not be null
        renderText("{'err':'', 'msg':{'ur':'/uploaded/xxx.zip'}}"); 
    } 
}

It works fine in IE6, the filedata is not null and contains the correct data. But, if I use chrome or firefox, the filedata is null!!

I use firebug to monitor what the firebug submit, and found it submit such a header:

content-disposition
attachment; name="filedata"; filename="051111twdns.zip"

I think play has not handle this case correctly, so the parameter "filedata" is null.

In order to work with chrome and firefox, I modified that action:

public class Application extends Controller {
    public static void upload(File filedata) { 
        if(filedata!=null) {
            // ok, it's IE6
            renderText("{'err':'', 'msg':{'ur':'/uploaded/xxx.zip'}}"); 
        } else {
            // it's chrome or firefox, the data is in request.body
            File targetFile = new File("upload/test.zip");
            IOUtils.copy(request.body, new FileOutputStream(targetFile));
        }
    } 
}

This is worked in IE6, chrome and firefox now, BUT, only if the upload file is very small. E.g. less than 4K. If it's a little larger, e.g. 12K, the method "IOUtils.copy" will report "Read Error!", even the following code will report such error:

request.body.available()
request.body.read()
request.body.read(bytes)
like image 501
Freewind Avatar asked Oct 05 '10 12:10

Freewind


People also ask

How do I upload a file to play framework?

The standard way to upload files in a web application is to use a form with a special multipart/form-data encoding, which lets you mix standard form data with file attachment data. Note: The HTTP method used to submit the form must be POST (not GET ). The getRef() method gives you a reference to a TemporaryFile .

How do I upload files to playwright?

To upload a file using Playwright use setInputFiles(selector, files[, options]) function. This method takes the selector of the input element and the path to the file you want to upload. The files parameter value can be a relative path (relative to the current working directory) or an absolute path.

What is multipart file upload?

What is Multipart Upload Request? ​ A multipart request is a HTTP request that HTTP clients construct to send files and data over to a HTTP Server. It is commonly used by browsers and HTTP clients to upload files to the server.

How do I upload files to spring boot?

Spring Boot file uploader Create a Spring @Controller class; Add a method to the controller class which takes Spring's MultipartFile as an argument; Save the uploaded file to a directory on the server; and. Send a response code to the client indicating the Spring file upload was successful.


1 Answers

Try integrate your site with file uploader ,which have a lof of documentation/samples for diffrent languages www.uploadify.com/

like image 180
John Avatar answered Oct 21 '22 04:10

John