所以我设法使用自定义指令通过Angular.js将图像上传到我的服务器。我还设法实现Cordova的相机功能。现在我想将图像发送到服务器
我的控制员:
$scope.getPhoto = function() {
console.log("foncton of picture");
navigator.camera.getPicture(onSuccess, onFail, { quality: 75, targetWidth: 320,
targetHeight: 320, destinationType: 0 });
//destination type was a base64 encoding
function onSuccess(imageData) {
console.log("success");
//preview image on img tag
$('#image-preview').attr('src', "data:image/jpeg;base64,"+imageData);
console.log("success imageData");
console.log(imageData);
//setting scope.lastPhoto
$scope.lastPhoto = dataURItoBlob("data:image/jpeg;base64,"+imageData);
//$localStorage.lastPhoto=dataURItoBlob("data:image/jpeg;base64,"+imageData);
console.log("$localStorage.lastPhoto");
console.log(imageData);
}
function onFail(message) {
alert('Failed because: ' + message);
}
}
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString = atob(dataURI.split(',')[1]);
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++)
{
ia[i] = byteString.charCodeAt(i);
}
var bb = new Blob([ab], { "type": mimeString });
return bb;
}
var objInsc = new Object();
objInsc.imageJson=$scope.lastPhoto;
$http.post('http://@ip:8080/elodieService/evenements/',objInsc).success(function(response, status, headers, config){
alert("SUCCESS ajout dans la table evenement!!");
});
我发现imageJson为null
我该怎么办,请帮助我:(
答案 0 :(得分:1)
我已经在我的一个项目中实现了这个功能,我将告诉你如何做到这一点,它需要付出很多努力才能使它工作。
首先:将图像uri转换为base64为mime字符串
这是向服务器发送图片最棘手的部分。
var encodeImageUri = function(imageUri) {
var deferred = $q.defer();
var c = document.createElement("canvas");
var ctx = c.getContext("2d");
var img = new Image();
img.onload = function() {
c.width = this.width;
c.height = this.height;
ctx.drawImage(img, 0, 0);
var dataURI = c.toDataURL("image/jpeg");
var byteString = atob(dataURI.split(",")[1]);
var mimeString = dataURI.split(",")[0].split(":")[1].split(";")[0];
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
var bb = new Blob([ab], { "type": mimeString });
deferred.resolve(bb);
};
img.src = imageUri;
return deferred.promise;
};
第二:编写send方法将其发送到服务器:
var sendPhoto = function (sessionID, photoURI) {
var deferred = $q.defer();
if (photoURI == null || photoURI == "") {
deferred.resolve(true);
}
encodeImageUri(photoURI).then(function(photoData) {
var fd = new FormData();
fd.append("PhotoData", photoData);
$http.post("*****YOUR URL******", fd, {
headers: {
'Content-Type': undefined
}
})
.success(function(data, status, headers) {
deferred.resolve(data);
})
.error(function(data, status, headers) {
deferred.reject(data);
});
});
return deferred.promise;
};
说明:
1.您从 $ q 服务创建延迟对象。
2.检查photoUrl的空引用。
3.您创建FormData,因为Photos需要作为formData发送。
3.拨打http post,并将标题的content-type
更改为 undefined
4.处理回电
5.回报诺言。