Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file from Javascript to Google Cloud Endpoint

I'm creating a web app using only HTML5 + Javascript + jQueryMobile and I wanted to upload a file to a Google App Engine web application using a Google Cloud Endpoint, also created by me.

As I control both sides, I can (and want to) create the simplest interaction possible.

As for the Endpoint, I thought of creating a method like this:

@ApiMethod(
  name = "uploadFile",
  path = "upload_file",
  httpMethod = HttpMethod.POST
)
public void uploadFile(File file) {
  //process the file
}

This File class could contain a field fileData of type Blob, or byte[] or something like that, repersenting the file data... Something like:

public class File {    
  private String fileName;
  private long fileSize;
  private Blob fileData;    
  //getters and setters
}

So the first question would be: what's the most suitable type for this field fileData?

And, taking into account the type selected for the field, how could I create the necessary POST request for that endpoint method form Javascript/jQuery?

Basically I need to create a POST request to http://myappid.appspot.com/_ah/api/files/v1/upload_file adding the File object in the POST data.

Note: I'm sorry I haven't tried anything for the Javascript code because I'm not familiar at all with this technologies, so I'd appreciate any help...

like image 381
MikO Avatar asked Apr 08 '13 13:04

MikO


People also ask

Can we upload file using JavaScript?

html file through a browser, the client will be able to upload a file to the server using Ajax and pure JavaScript. A pure JavaScript file uploader simplifies Ajax based interactions with the server.

Can I upload files to Google Cloud Storage from URL?

Uploading files to Google Cloud Storage from a URL is possible, but there are a few things to keep in mind. First, you'll need to create a Google Cloud Storage bucket and give it a name. Next, you'll need to create a file object in the bucket and provide the URL of the file you want to upload.


1 Answers

Edit: The answer below targes python version of AppEngine

It is a common demand with no clear solution. Till now, gae-init-upload is a demonstration of how you can achieve that with AppEngine and CoffeeScript. Worth having a look, CoffeeScript is being compiled into JavaScript in case you are not familiar.

The JavaScript solution you are looking for is under

/main/static/src/coffee/common/upload.coffee

like image 169
topless Avatar answered Sep 30 '22 18:09

topless