duansengcha9114 2014-12-31 23:00
浏览 36

将图像从iOS应用程序上传到谷歌应用引擎和存储桶存储PHP

I'm trying to upload an image sent from my iOS app to my bucket storage for app engine and I'm using php. Now let me be clear the example google gave with the web form works perfectly with my handler and it is commented out below So I'm sure my handler is fine and dandy also it works directly with my handler before using app engine and the storage bucket. For anyone unfamiliar with createUploadUrl Here is the documentation on it.

<?php 
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';

use google\appengine\api\cloud_storage\CloudStorageTools;

$options = [ 'gs_bucket_name' => 'my-bucket-name' ];

$upload_url = CloudStorageTools::createUploadUrl('/CustomPostHandler.php', $options);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: multipart'
        )
);

$context = stream_context_create($opts);

$result = file_get_contents($upload_url,false,$context);

echo $result;
/*<form action="<?php echo $upload_url?>" enctype="multipart/form-data" method="post">
    Files to upload: <br>
   <input type="file" name="userfile" size="10000000">
   <input type="submit" value="Send">
</form>*/

?>

Because i want to use storage bucket i call the createUploadUrl because thats what google wants. I'm using swift on the iOS front end and the error code I'm getting for echoing the $result is "The request's content type is not accepted on this url" I have tried to use 'multipart/form-data' for the php part and it gave me the error 'Bad type use multipart' or something along those lines.

the iOS code is posted below and again ill say this works perfectly when not running on app engine but on a local server. The error message is my obvious problem but i think specifically it may have something to do with the octet-stream I'm not sure. Any help would be appreciated!!! Thank you!

var postString = "\(PHP_PATH)UploadTest.php"
        
        var request = NSMutableURLRequest(URL: NSURL(string: postString)!)
        
        request.HTTPMethod = "POST"
        
        var boundary = "---------------------------14737809831466499882746641449"
        
        var contentType = "multipart/form-data; boundary=\(boundary)"
        
        request.addValue(contentType, forHTTPHeaderField: "Content-Type")
        
        println(request)
        
        var body = NSMutableData()
        
        body.appendData(("
--\(boundary)
" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
        
        body.appendData(("Content-Disposition: form-data; name=\"userfile\"; filename=\"\(fileName)\"
" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
        
        body.appendData(("Content-Type: application/octet-stream

" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
        
        body.appendData(imageData)
        
        body.appendData(("
--\(boundary)--
" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
        
        request.HTTPBody = body
        
        var returnData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
        
        println(NSString(data: returnData!, encoding: NSUTF8StringEncoding)!)

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?