因此,我设法使用自定义指令通过 Angular.js 将图像上传到我的服务器。
我还成功地实现了 Cordova 的相机功能。
现在我尝试连接两者,但是当将图像发送到服务器时,它们被存储为空。我认为问题在于我使用输入字段来获取图像,并且它获取了完整的对象,而现在我在拍照并发送后只获取了图像路径。我的问题是,我真的不明白如何将路径转换为对象,如果这就是问题所在?
索引.html
<form class="form-horizontal" role="form">
<button class="picButton" ng-click="getPhoto()" class="button button-block button-primary">Take Photo</button>
<img ng-src="{{lastPhoto}}" style="max-width: 100%">
...
</form>
控制器.js
$scope.getPhoto = function() {
Camera.getPicture().then(function(imageURI) {
console.log(imageURI);
$scope.lastPhoto = imageURI;
$scope.upload(); <-- call to upload the pic
},
function(err) {
console.err(err);
}, {
quality: 75,
targetWidth: 320,
targetHeight: 320,
saveToPhotoAlbum: false
});
};
$scope.upload = function() {
var url = '';
var fd = new FormData();
//previously I had this
//angular.forEach($scope.files, function(file){
//fd.append('image',file)
//});
fd.append('image', $scope.lastPhoto);
$http.post(url, fd, {
transformRequest:angular.identity,
headers:{'Content-Type':undefined
}
})
.success(function(data, status, headers){
$scope.imageURL = data.resource_uri; //set it to the response we get
})
.error(function(data, status, headers){
})
}
打印 $scope.lastPhoto 时我得到图像路径:文件:///var/mobile/Applications/../tmp/filename.jpg
EDIT
请求标头:
------WebKitFormBoundaryEzXidt71U741Mc45
Content-Disposition: form-data; name="image"
file:///var/mobile/Applications/C65C8030-33BF-4BBB-B2BB-7ECEC86E87A7/tmp/cdv_photo_015.jpg
------WebKitFormBoundaryEzXidt71U741Mc45--
这应该引起问题吗?因为我可以看到我发送的是路径,但不是图像本身。
在更改之前,我使用自定义指令,并使用 $scope.files 附加到上传函数中的请求:
<input type="file" file-input="files" multiple onchange="angular.element(this).scope().filesChanged(this);upload();">
<button ng-click="upload()">Upload</button>