我正在尝试使用纯粹的javascript将图片上传到Google Blobstore。我有一个REST api,我查询以获取上传URL。我抓住上传网址并使用以下代码发布:
<form id="newmemberAccount" class="form-horizontal" enctype="multipart/form-data" role="form">
<div class="form-group">
<label for="fileUpload" class="col-sm-2 control-label">Upload Picture</label>
<div class="col-sm-10">
<input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/>
</div>
</div>
<button type="submit" ng-click="uploadPicture()" class="btn btn-default pull-right">Submit</button>
然后我通过Angular控制器中的以下代码获取信息:
$scope.uploadFile = function(files) {
newprofileImage = new FormData();
//Take the first selected file
newprofileImage.append("file", files[0]);
}
$scope.uploadPicture = function(){
console.log(newprofileImage);
$http.post($scope.url, newprofileImage, {
headers: {'Content-Type': undefined},
transformRequest: angular.identity,
})
.success(function(data){
console.log("success");
console.log(data);
})
.error(function(data) {
console.log("failure");
console.log(data);
});
}
当我运行post请求时,我收到了404响应,但我可以看到图像已上传到app引擎控制台中的blob。所以我真的很困惑我做错了什么。我需要获得响应,以便我可以找到托管图像的位置。下面我将发布从Chrome控制台获取的http请求发送的内容。 请求标题:
host:greek-app.appspot.com
:method:POST
:path:/_ah/upload/AMmfu6ZMBz-K8oR9rk5OWfcp2Q0RO2xAJgAgAzCkECtoc0X7yR2rFkRVxp6zk_hsEbBfbxVYMO9019P34-3NJpMH0j40pnaOrhbvK8aBtIVG6nV3IBiNg11RkvWxib7R91AKILGOI5Mg/ALBNUaYAAAAAU4YdODoOR0AThPmQmDt8Xzv7cpEY4H33/
:scheme:https
:version:HTTP/1.1
accept:application/json, text/plain, */*
accept-encoding:gzip,deflate,sdch
accept-language:en-US,en;q=0.8
cache-control:no-cache
content-length:156171
content-type:multipart/form-data; boundary=----WebKitFormBoundaryZtJNWWPvaj71mTbg
cookie:USER_NAME=derek.wene; TOKEN=92576e74-f3dc-4174-9d70-e1bd9fffb7c0
origin:https://greek-app.appspot.com
pragma:no-cache
referer:https://greek-app.appspot.com/
请求有效负载:
------WebKitFormBoundaryZtJNWWPvaj71mTbg
Content-Disposition: form-data; name="file";
filename="10338463_10202806339897092_340454719367107135_o.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryZtJNWWPvaj71mTbg--