首页 文章

使用PhoneGap Google App Engine上传和保存图像

提问于
浏览
1

Goal: 在PhoneGap应用程序中拍摄/附加图片,并将每张图片的公共URL发送到Google Cloud SQL数据库 .

Question 1: 有没有办法从base64编码的图像(在Python中)创建Google Cloud 端存储对象,然后将该对象上传到存储桶并返回公共链接?

我希望使用PhoneGap将图像发送到Python Google App Engine应用程序,然后让该应用程序将图像发送到我设置的Google Cloud Storage存储桶,然后将公共链接返回给PhoneGap应用程序 . 这些图像可以直接从应用程序中获取,也可以从用户设备上的现有照片中附加 .

我使用PhoneGap的FileTransfer插件将图像上传到GAE,GAE作为base64编码图像发送(这不是我可以控制的) .

根据我在Google Docs中找到的内容,我可以将图像上传到Blobstore;但是,它需要一个表单中的 <input type='file'> 元素 . 我没有't have ' file ' input elements; I just take the image URI returned from PhoneGap' s相机对象并显示拍摄(或附加)的图片的缩略图 .

Question 2: 是否可以拥有 <input type='file'> 元素并根据用户选择文件或拍照来控制's value? As in, is it possible to set it'的值?

提前致谢!

2 回答

  • 2

    这是针对可能面临此问题的其他人的解决方案 . 事实证明它非常简单!

    为GAE项目设置存储桶后,可以使用此Python代码将图像发送到存储桶:

    import cloudstorage as gcs
    import webapp2
    import cgi
    import MySQLdb
    import os
    import logging
    import time
    
    from google.appengine.api import mail
    from google.appengine.api import images
    from google.appengine.ext import blobstore
    
    class UploadImageHandler(webapp2.RequestHandler):
        def post(self):
            self.response.headers.add_header(ACCESS_CONTROL, '*')
    
            f = self.request.POST['image']
            fname = '/your-bucket-name/%s' % f.filename;
    
            gcs_file = gcs.open(fname, 'w', content_type="image/jpeg")
            gcs_file.write(self.request.get('image'))
            gcs_file.close()
    

    用于从PhoneGap应用程序上传文件的代码:

    // Uploads images in "imageURIs" to the web service specified in "server".
    function uploadImages(imageURIs, server) {
        var success = function(data) {
            alert("Successfully uploaded image!");
        };
    
        var fail = function(error) {
            alert("Failed to upload image: "+error);
        };
    
        var options = new FileUploadOptions();
        options.fileKey = "image";
        options.mimeType = "image/jpeg";
        var ft = new FileTransfer();
    
        for (var i = 0; i < imageURIs.length; i++) {
            alert("Uploading"+i);
            options.fileName = imageURIs[i].substr(imageURIs[i].lastIndexOf('/') + 1);
            ft.upload(imageURIs[i], encodeURI(server), success, fail, options);
        }
    }
    

    我希望它可以帮助别人 . :)

  • 0

    是的,这对GAE和GCS来说很好用 . 你本身不需要 <input type=file> . 您只需在对GAE网址的调用中设置 POST 参数即可 . 确保您也发送隐藏密钥,并使用SSL加密的网址,以防止垃圾邮件发送者发布到您的应用程序 .

相关问题