0

Is there any way to make ONE request with multiple upload in IONIC 3? My server side is PHP codeigniter.

this is my code:

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
  })
}
6
  • share your ionic code . Commented May 18, 2018 at 3:05
  • @mohamedvall I edit my post Commented May 18, 2018 at 14:20
  • you can use for-loop for that Commented May 19, 2018 at 10:24
  • Thanks for your help guys! I figure it out I put it like this instead. uploadPhoto = []; let options : FileUploadOptions = { fileKey: 'file', fileName: 'name.jpg', headers: {}, mimeType: "image/jpeg", params: { 'model': uploadPhoto, } Commented May 25, 2018 at 18:49
  • hey @MelPogz, does this supports multiple file upload with file transfer? what is the type of this.photos variable? Commented Oct 29, 2018 at 11:01

2 Answers 2

1

Here is how I did this with Ionic 3 (in my case I am using images):

//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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Solution by OP.

I figure it out I put it like this instead.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.