[英]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. 我正在仅使用HTML5 + Javascript + jQueryMobile创建一个Web应用程序,并且想使用我创建的Google Cloud Endpoint将文件上传到Google App Engine Web应用程序。
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: 这个
File
类可以包含Blob类型或byte []之类的字段fileData
,或类似的东西,表示文件数据...诸如此类:
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
? 因此,第一个问题是: 此字段
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? 而且,考虑到为该字段选择的类型, 我如何为该终结点方法使用Javascript / jQuery创建必要的POST请求?
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. 基本上,我需要向
http://myappid.appspot.com/_ah/api/files/v1/upload_file
创建POST请求,并在POST数据中添加File
对象。
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... 注意:很抱歉,我没有为Javascript代码做任何尝试,因为我对这种技术一点都不熟悉,因此,我将不胜感激。
Edit: The answer below targes python version of AppEngine 编辑:下面的答案以AppEngine的python版本为目标
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.
到目前为止, gae-init-upload演示了如何使用AppEngine和CoffeeScript实现该目标。 Worth having a look, CoffeeScript is being compiled into JavaScript in case you are not familiar.
值得一看的是,如果您不熟悉,CoffeeScript将被编译为JavaScript。
The JavaScript solution you are looking for is under 您正在寻找的JavaScript解决方案
/main/static/src/coffee/common/upload.coffee
I eventually used this code in my AMD Javascript application. 我最终在我的AMD Javascript应用程序中使用了此代码。 I'm sorry I cannot explain it too much because I've written a big amount of code since I wrote this project, and as you can see I didn't comment the code properly ( fail!! ), anyway maybe you can get some ideas...
对不起,我不能解释太多,因为自从我写了这个项目以来,我已经写了很多代码,并且如你所见,我没有正确地注释代码( 失败! ),无论如何,你可能会得到一些想法...
Note that there's something about getting navigator position because I wanted to store the location where the file was uploaded from, but it's not necessary at all! 请注意,获取导航器位置有些事情,因为我想存储文件上传的位置,但这根本没有必要!
uploadFile: function(request, render) {
var self = this;
var file = $("#file").get(0).files[0];
var reader = new FileReader();
reader.onload = function (evt) {
var upload = {
provider: self.folder.provider,
folderIdentifier: self.folder.id,
fileName: file.name,
fileSize: file.size,
base64Data: btoa(evt.target.result),
location: {
latitude: self.position.coords.latitude,
longitude: self.position.coords.longitude
}
}
var uploadFilePromise = self.connector.uploadFile(self.sessionToken.token, upload);
uploadFilePromise.done(function (file) {
render("file", {
result: "DONE",
file: file
});
});
uploadFilePromise.fail(function (error) {
render("file", {
result: "FAIL"
});
});
}
navigator.geolocation.getCurrentPosition(function(position) {
self.position = position;
reader.readAsBinaryString(file);
});
}
uploadFile: function (sessionToken, upload) {
var self = this;
var promise = new Promise();
gapi.client.load('upload', 'v1', function() {
var request = gapi.client.upload.uploadFile({
session_token: sessionToken,
resource: upload
});
request.execute(function(response) {
if (response.error) {
promise.reject(response.error);
}
else {
var file = File.create(response.result.provider,
response.result.type,
response.result.identifier,
response.result.name,
response.result.description,
response.result.created,
response.result.size,
response.result.link,
{
latitude: response.result.location.latitude,
longitude: response.result.location.longitude
});
promise.resolve(file);
}
});
}, self.api);
return promise;
}
@Api(name="upload")
public class UploadEndpoint {
@ApiMethod(
name = "uploadFile",
path = "upload_file",
httpMethod = HttpMethod.POST
)
public File uploadFile (
@Named("session_token") String token,
Upload upload) throws InternalServerErrorException {
File file = new UploadController().uploadFile(token, upload);
return file;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.