Upload file from Javascript to Google Cloud Endpoint

后端 未结 2 923
無奈伤痛
無奈伤痛 2021-02-09 21:55

I\'m creating a web app using only HTML5 + Javascript + jQueryMobile and I wanted to upload a file to a Google App Eng

相关标签:
2条回答
  • 2021-02-09 22:26

    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

    0 讨论(0)
  • 2021-02-09 22:44

    I eventually used this code in my AMD Javascript application. 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!

    Controller.js

        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);
            });
        }
    

    Connector.js

        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;
        }
    

    Endpoint.java

    @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;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题