从iphone上传图片到GAE blobs

2024-05-18 21:41:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了整个上午的时间寻找一个清晰的例子来说明如何将用iPhone拍摄的照片上传到blobstore,但是没有成功。在

目前我已经开发了我的iPhone应用程序,可以用PHP将图片发送到服务器,服务器中有以下代码:

// Function to upload a photo in a file and save data in the DB
function upload($photoData, $descr, $phone) {
    // Folder to upload data
    $path = $_SERVER['DOCUMENT_ROOT']."/program/data/";

    // Check if there was no error during the file upload
    if ($photoData['error'] == 0) {
        $result = query("INSERT INTO pics(descr, phone) VALUES('%s','%s')", $descr, $phone);
        if (!$result['error']) {
            // Inserted in the database, go on with file storage
            // Obtain database link (in lib.php)
            global $link;

            // Get the last automatically generated ID
            $idPhoto = mysqli_insert_id($link);

            // Move the temporarily stored file to a convenient location
            if (move_uploaded_file($photoData['tmp_name'], $path.$idPhoto.".jpg")) {
                // File moved, all good, generate thumbnail
                thumb($path.$idPhoto.".jpg", 180);
                print json_encode(array('successful' => 1));
            } else {
                errorJson('Upload on server problem');
            }
        } else {
            errorJson('Save database problem: '.$result['error']);
        }
    } else {
        errorJson('Upload malfunction.');
    }
}

Objective-C中实现这一点的部分是(我使用的是AFNetworking,对象API sharedInstance是一个AFJSONRequestOperation类):

^{pr2}$

这很好地工作,图像保存在服务器上。但是我想用数据存储也一样,你不能保存文件。所以我做了一个网页来练习如何保存图片,我可以从一个标准的网页表单上传图片而不会有任何问题。这是我用来将它保存在GAE中的代码(忘记我自己的助手类或函数,比如PicturePageHandler或render_page):

# Get and post for the create page
class Create(PicturePageHandler, blobstore_handlers.BlobstoreUploadHandler):
    def get(self):
        if self.user_logged_in():
            # The session for upload a file must be new every reload page
            uploadUrl = blobstore.create_upload_url('/addPic')
            self.render_page("addPicture.htm", form_action=uploadUrl)

    def post(self):
        if self.user_logged_in():
            # Create a dictionary with the values, we will need in case of error
            templateValues = self.template_from_request()
            # Test if all data form is valid
            testErrors = check_fields(self)

            if testErrors[0]:
                # No errors, save the object
                try:
                    # Get the file and upload it
                    uploadFiles = self.get_uploads('picture')
                    # Get the key returned from blobstore, for the first element
                    blobInfo = uploadFiles[0]
                    # Add the key and the permanent url to the template
                    templateValues['blobKey'] = blobInfo.key()
                    templateValues['servingUrl'] = images.get_serving_url(blobInfo.key(), size=None)

                    # Save all
                    pic = Picture.save(self.user.key, **templateValues)
                    if pic is None:
                        logging.error('Picture save error.')

                    self.redirect("/myPics")

                except:
                    self.render_page("customMessage.htm", custom_msg=_("Problems while uploading the picture."))
            else:
                # Errors, render the page again, with the values, and showing the errors
                templateValues = custom.prepare_errors(templateValues, testErrors[1])
                # The session for upload a file must be new every reload page
                templateValues['form_action'] = blobstore.create_upload_url('/addPic')

                self.render_page("addPicture.htm", **templateValues)

我的问题是:

  1. 我可以继续使用Objective-C JSON调用将图片上载到服务器,还是必须完全更改上载图片的方式?在
  2. 如果可能的话,如何更改Python服务器代码以从JSON获取图片?在

Tags: andthekeyinself服务器ifpage

热门问题