在一个请求IONIC中上传多个文件

时间:2018-05-17 21:45:25

标签: php codeigniter file-upload ionic3 ionic-native

有没有办法在IONIC 3中多次上传一个请求?我的服务器端是PHP codeigniter。

这是我的代码:

upload(){
  const fileTransfer: FileTransferObject = this.transfer.create();

  var uploadPhoto = JSON.stringify( this.photos );

 let options : FileUploadOptions = {

    fileKey: 'file',
    fileName: 'name.jpg',
    headers: {},
    params: {
      'model': uploadPhoto
    }
 };

 console.log( JSON.stringify( options ) )

 fileTransfer.upload(this.photos, encodeURI(this.authService.apiURL + '/serviceupload'), options)
  .then(data=> {
    console.log( JSON.stringify( data),"data" );
  }, (err) => {
    // error
  })
}

2 个答案:

答案 0 :(得分:1)

这是我使用Ionic 3进行的操作(在我的情况下,我正在使用图像):

//declare my payload
var payload = {
      img:[]
    }

//add images to your payload, not file path to image but string data of image, file_uri comes from camera
payload.img.push(file_uri.toString())

//passing one parameter in the http.post and stringify the payload
this.http.post('https://www.myserver.com/submit.php?jobId=' + this.job.id, JSON.stringify(payload))

//PHP code to save images to file system
//get contents of request
$postdata = file_get_contents("php://input");

//we performed JSON.stringify before sending, need to decode here
$request = json_decode($postdata,true);

//loop through the array and save to file system with unique name
foreach($request['img'] as $data){

//next two lines are to get image text, removing "data:image/jpeg;base64,"
$base_to_php = explode(',', $data);
$data = base64_decode($base_to_php[1]);

//generate unique file name
$fileName = uniqid();

//save file on file system
file_put_contents('./uploads/' . $_REQUEST['job'] . '/142-' . $fileName . '.jpg',$data);
}

答案 1 :(得分:1)

OP解决方案。

我弄清楚了,我改成这样了。

uploadPhoto = [];
let options : FileUploadOptions = {
    fileKey: 'file',
    fileName: 'name.jpg',
    headers: {},
    mimeType: "image/jpeg",
    params: { 'model': uploadPhoto, }