使用FileTransfer上传图像时出现问题 [英] Trouble to upload images with FileTransfer

查看:542
本文介绍了使用FileTransfer上传图像时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中添加一个功能,允许用户使用相机或智能手机拍照,将其上传为个人照片。



<我已经从另一个项目中选择了这个功能,这个项目效果很好但不是我的,而是我的同事之一(他离开了)。



我不知道知道为什么,因为我使用了完全相同的功能,但上传的图像如下所示:



损坏的图片



它将随机开始破坏文件,通常在第一季度。有一次,它有效,但我无法理解为什么。

  takePhoto(value){

const选项: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是base64编码的字符串或文件URI
//如果它是base64:
让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,
标题:{
//连接:关闭
}
};

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)=> ; {

})

},(错误)=> {
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);
//将上传图像的路径
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:photobienupupée};
});
});

};



通过尝试Salman Ullah回答:

  POST / add / photoandroid / 5a1fd1a89a6f09412b6a74d7 500 28.912 ms  -  1426 _stream_readable.js:598 
dest。结束();
^
TypeError:无法在IncomingMessage.onend读取未定义
的属性'end'(_ stream_readable.js:598:9)
at Object.onceWrapper(events.js:314) :30)
在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
扔掉; //未处理的'错误'事件
^
错误:读取ECONNRESET
at_errnoException(util.js:1041:11)
在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)=> {


I’m trying to add a function to my application which allow the user to take a picture with his camera or from his smartphone to upload it as a profil picture.

I’ve picked this function from another project which is working great but isn’t from me but one of my colleague (he left).

I don’t know why since I’ve used the exact same function but the image uploaded look like this :

the corrupt image

It will randomly start to corrupt the file, generally in the first quarter. One time, it worked but I can’t figure why.

  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);
});

}

Any idea why it is doing this ? Oh, forgot to mention : with the iOS version of the app, uploading images is working great so I don't think the problem comes from the server properties. The iOS app uses its own function to retrieve the image on the server.

And here is server side :

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 "};
  });
});

};

By trying with Salman Ullah answer :

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)

This is the error I get from the server.

解决方案

Try senging imageData instead of targetPath.

Like this:

fileTransfer.upload(imageData ,"http://XX.XXX.XXX.XX:90/add/photoandroid/" + 
this.user._id, options) .then((data) => {

这篇关于使用FileTransfer上传图像时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆