Несколько загрузок файлов в одном запросе IONIC - PullRequest
0 голосов
/ 18 мая 2018

Есть ли способ сделать ОДИН запрос с многократной загрузкой в ​​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 голосов
/ 01 января 2019

Решение по ОП.

Я понял, что вместо этого я сказал это.

uploadPhoto = [];
let options : FileUploadOptions = {
    fileKey: 'file',
    fileName: 'name.jpg',
    headers: {},
    mimeType: "image/jpeg",
    params: { 'model': uploadPhoto, }
0 голосов
/ 31 августа 2018

Вот как я это сделал с 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);
}
...