Cordova 使用FileTransfer上载图像时遇到问题
我正在尝试向我的应用程序添加一个功能,允许用户使用相机拍照,或者从智能手机上传profil图片 我从另一个项目中选择了这个功能,这个项目工作得很好,但不是我的,而是我的一个同事(他离开了) 我不知道为什么,因为我使用了完全相同的功能,但上传的图像如下所示: 通常在第一季度,它会随机开始损坏文件。有一次,它奏效了,但我不明白为什么Cordova 使用FileTransfer上载图像时遇到问题,cordova,ionic-framework,file-transfer,Cordova,Ionic Framework,File Transfer,我正在尝试向我的应用程序添加一个功能,允许用户使用相机拍照,或者从智能手机上传profil图片 我从另一个项目中选择了这个功能,这个项目工作得很好,但不是我的,而是我的一个同事(他离开了) 我不知道为什么,因为我使用了完全相同的功能,但上传的图像如下所示: 通常在第一季度,它会随机开始损坏文件。有一次,它奏效了,但我不明白为什么 takePhoto(value){ const options: CameraOptions = { quality: 75, destinationT
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)
这是我从服务器收到的错误。请尝试发送imageData而不是targetPath 像这样:
fileTransfer.upload(imageData ,"http://XX.XXX.XXX.XX:90/add/photoandroid/" +
this.user._id, options) .then((data) => {