我正在尝试在我的应用程序中添加一个功能,允许用户使用相机或智能手机拍摄照片,将其上传为个人照片。
我从另一个项目中选择了这个功能,这项功能很好但不是我的,而是我的同事(他离开了)。
我不知道为什么因为我使用了完全相同的功能,但上传的图像如下所示:
它将随机开始破坏文件,通常在第一季度。有一次,它有效,但我无法理解为什么。
takePhoto(value){
const options: CameraOptions = {
quality: 75,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
correctOrientation : true,
allowEdit:true,
targetWidth: 300,
targetHeight: 300,
sourceType : value
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64:
let base64Image = 'data:image/jpeg;base64,' + imageData;
console.log(base64Image)
//$("#imgProfil").attr("src",base64Image);
const fileTransfer: FileTransferObject = this.transfer.create();
this._zone.run(() => {
this.avatar_path = base64Image;
});
var targetPath = base64Image;
var options = {
fileKey: "avatar",
fileName: this.user._id + ".jpg",
chunkedMode: false,
mimeType: "image/jpeg",
headers: {
//Connection: "close"
}
};
fileTransfer.upload(targetPath,"http://XX.XXX.XXX.XX:90/add/photoandroid/" + this.user._id, options) .then((data) => {
this.storage.set('photoProfilData',base64Image);
this.events.publish('photoProfil');
}, (err) => {
})
}, (err) => {
this.presentToasti(err);
});
}
知道为什么会这样做吗? 哦,忘了提及:使用iOS版本的应用程序,上传图像效果很好所以我不认为问题来自服务器属性。 iOS应用程序使用自己的功能来检索服务器上的图像。
这是服务器端:
exports.addphotoandroid = function(req, res){
var ObjectID = require('mongodb').ObjectID;
var fstream;
var monid = req.params.id;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
console.log("Uploading: " + monid + ".jpg");
//Path where image will be uploaded
fstream = fs.createWriteStream("/var/sammy/public/upload/photoprofil/" + monid + ".jpg");
file.pipe(fstream);
fstream.on('close', function () {
console.log("Upload Finished of " + monid + ".jpg");
response = {"succces" : "photo bien uploadée "};
});
});
};
尝试Salman Ullah回答:
POST /add/photoandroid/5a1fd1a89a6f09412b6a74d7 500 28.912 ms - 1426 _stream_readable.js:598
dest.end();
^
TypeError: Cannot read property 'end' of undefined
at IncomingMessage.onend (_stream_readable.js:598:9)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:105:13)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1059:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
events.js:182
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at _errnoException (util.js:1041:11)
at Pipe.onread (net.js:606:25)
这是我从服务器获得的错误。
答案 0 :(得分:0)
尝试使用imageData而不是targetPath。
像这样:
fileTransfer.upload(imageData ,"http://XX.XXX.XXX.XX:90/add/photoandroid/" +
this.user._id, options) .then((data) => {