Ionic 3 file select and upload giving error

2019-08-25 16:52发布

问题:

when selecting image from gallary to upload, it gives error. Below is my php code and home.ts code

Error is at php side or ionic ?

php code

<?php
header('Access-Control-Allow-Origin: *');
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
    echo "Upload and move success";
} else {
echo $target_path;
    echo "There was an error uploading the file, please try again!";
}
?>

Home.ts code

import { Component } from '@angular/core';
import { NavController,NavParams } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';

//trying to upload file from below
import { File } from '@ionic-native/file';
import { FileChooser } from '@ionic-native/file-chooser';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

//import { UserProfileService } from '../../services/login.service';
//import { ConfirmidentityPage } from '../confirmidentity/confirmidentity';
//import { ProgressDialog } from '../../utility/progress-dialog';



@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController, private builder: FormBuilder, private transfer: FileTransfer,private camera: Camera, private file: File,private fileChooser: FileChooser) {

  }

  upload()
  {

     let options = {

         quality: 100
          };


    this.camera.getPicture(options).then((imageData) => {
     // imageData is either a base64 encoded string or a file URI
     // If it's base64:

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

    let options1: FileUploadOptions = {
       fileKey: 'file',
       fileName: 'name.jpg',
       headers: {}

    }

fileTransfer.upload(imageData, 'http://sco7.com/del/uploadtest/upload-device.php', options1)
 .then((data) => {
   // success
   alert("success");
 }, (err) => {
   // error
   alert("error"+JSON.stringify(err));
 });


  });
}


openGallery() {
  var options = {
     sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
     destinationType: this.camera.DestinationType.FILE_URI
   };
   this.camera.getPicture(options).then((imageData)  => {
      // imageData is either a base64 encoded string or a file URI
      // If it's base64:

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

     let options1: FileUploadOptions = {
        fileKey: 'image_upload_file',
        fileName: 'name.jpg',
        headers: {},
        params: {"app_key":"Testappkey"},
        chunkedMode : false

     }

 fileTransfer.upload(imageData, 'http://sco7.com/del/uploadtest/upload-device.php', options1)
  .then((data) => {
    // success
    alert("success"+JSON.stringify(data));
  }, (err) => {
    // error
    alert("error"+JSON.stringify(err));
  });
   });
}

Function upload() works when called from home.html button, image clicked is uploaded to server, but function openGallery() does not work.

When i select image and upload, it says -

success{"bytesSent":5433,"response Code":200,"response":"uploads/ There was an error uploading the file, please try again!","objectId":""}

Please help. Thanks in advance for your time.

回答1:

Judging by the error, you are not going to correctly $target_path - missing file name from basename( $_FILES['file']['name']). Most likely, it just does not reach PHP. I would change:

let options = {

    quality: 100,
    destinationType: this.camera.DestinationType.DATA_URL,
    encodingType: this.camera.EncodingType.JPEG,
    mediaType: this.camera.MediaType.PICTURE
      };


let options1: FileUploadOptions = {
   fileKey: 'file',
   fileName: 'name.jpg',
   headers: {},
   chunkedMode: false,
   httpMethod: 'post',
   mimeType: "image/jpeg"

}


fileTransfer.upload('data:image/jpeg;base64,' + imageData, 'http://sco7.com/del/uploadtest/upload-device.php', options1)