I have a javascript frontend connect to a Google App Engine backend. The instructions for uploading a file to Blobstore assume you are using JSPs, which I am not. The example for creating a file drop handler assume you just want to send the file up by itself in a post.
This works for sending the file up:
var files = event.getBrowserEvent().dataTransfer.files;
if (files.length > 0) {
if (files.length > 1) {
console.log('Too many files!');
} else {
var file = files[0];
var xhr = new goog.net.XmlHttp();
xhr.open('POST', '/imageUpload?gwt.codesvr=localhost:9997');
xhr.send(file);
}
}
But this is just sending the data directly to my servlet. I could add a form to my html but as far as I can tell closure doesn’t like setting inputs of type file.
Suggestions as to how I can upload my files to the Blobstore?
With some help from this answer, I finally figured this out.
First off, make sure sessions are enabled in your appengine-web.xml:
<sessions-enabled>true</sessions-enabled>I ended up writing two separate servlets, one that creates the upload URL and the second that handles the redirect after the image is written to the blobstore.
Image upload URL servlet
Client side image upload requestor
extractValidJson(event)is just a utility function I wrote to pull out the json response.Once we have a valid URL, we can allow uploads.
Client side upload drop sender
Image upload redirect servlet
}
Then you can do whatever in your uploadResponseHandler. Request finished is 4 (see this).
Not so bad once all the pieces come together.