File Upload via XHR

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

File Upload via XHR

kaiserfranz
Been up all night trying to get a drag&drop file upload running. Two part Question: Is xhr an option to send files to a controller (like this):
  var boundary = '---------------------------';
			boundary += Math.floor(Math.random()*32768);

			xhr.open(opts.method, opts.url);
			xhr.setRequestHeader("Cache-Control", "no-cache");
			xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			xhr.setRequestHeader("X-File-Name", file.fileName);
			xhr.setRequestHeader("X-File-Size", file.fileSize);
			xhr.setRequestHeader("Content-Type", 'multipart/form-data; boundary=' + boundary);
			
			builder = 'Content-Disposition: form-data; name="file"';
	        if (file.fileName) {
	          builder += '; filename="' + file.fileName + '"';
	        }
	        builder += crlf;

	        builder += 'Content-Type: image/jpeg';
	        builder += crlf;
	        builder += crlf; 

	        /* Append binary data. */
	        builder += file.getAsBinary();
	        builder += crlf;

	        /* Write boundary. */
	        builder += dashdash;
	        builder += boundary;
	        builder += dashdash;
	        builder += crlf;
		xhr.send(builder);
		/xhr.send(file);
As this seems to hit the server, i wonder why I can't acces the File - seems to be empty ?
def file = request.getFile("file");
		println file
		if(request instanceof MultipartHttpServletRequest)
		{
		  MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;  
		  CommonsMultipartFile f = (CommonsMultipartFile) mpr.getFile("file");

{...}
any ideas ?
Reply | Threaded
Open this post in threaded view
|

Re: File Upload via XHR

Jason Davis
i send files with xhr all the time. I use some library i found for it
though. AjaxUpload or something..

On Mon, Jan 31, 2011 at 9:34 PM, kaiserfranz <[hidden email]> wrote:

> Been up all night trying to get a drag&drop file upload running. Two part
> Question: Is xhr an option to send files to a controller (like this): var
> boundary = '---------------------------'; boundary +=
> Math.floor(Math.random()*32768); xhr.open(opts.method, opts.url);
> xhr.setRequestHeader("Cache-Control", "no-cache");
> xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
> xhr.setRequestHeader("X-File-Name", file.fileName);
> xhr.setRequestHeader("X-File-Size", file.fileSize);
> xhr.setRequestHeader("Content-Type", 'multipart/form-data; boundary=' +
> boundary); builder = 'Content-Disposition: form-data; name="file"'; if
> (file.fileName) { builder += '; filename="' + file.fileName + '"'; } builder
> += crlf; builder += 'Content-Type: image/jpeg'; builder += crlf; builder +=
> crlf; /* Append binary data. */ builder += file.getAsBinary(); builder +=
> crlf; /* Write boundary. */ builder += dashdash; builder += boundary;
> builder += dashdash; builder += crlf; xhr.send(builder); /xhr.send(file); As
> this seems to hit the server, i wonder why I can't acces the File - seems to
> be empty ? def file = request.getFile("file"); println file if(request
> instanceof MultipartHttpServletRequest) { MultipartHttpServletRequest mpr =
> (MultipartHttpServletRequest)request; CommonsMultipartFile f =
> (CommonsMultipartFile) mpr.getFile("file"); {...} any ideas ?
> ________________________________
> View this message in context: File Upload via XHR
> Sent from the Grails - user mailing list archive at Nabble.com.
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: File Upload via XHR

kaiserfranz
I have tried AjaxUpload,

same Problems - request on the server side is empty. Am I missing any request parameters - think it's quite picky about the format.
Reply | Threaded
Open this post in threaded view
|

Re: File Upload via XHR

Jason Davis
Hmm,, just works here

On Mon, Jan 31, 2011 at 9:54 PM, kaiserfranz <[hidden email]> wrote:

>
> I have tried AjaxUpload,
>
> same Problems - request on the server side is empty. Am I missing any
> request parameters - think it's quite picky about the format.
> --
> View this message in context: http://grails.1312388.n4.nabble.com/File-Upload-via-XHR-tp3250476p3250486.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email