upload image ionic 3

两盒软妹~` 提交于 2019-12-07 22:29:54

问题


I am developing an app in ionic 3 and need to upload image to server using an api created in lumen framework. The request to upload image is : The image is clicked from camera and converted to base64.

let base64Image = 'data:image/jpeg;base64,' + imageData;

Then i use FileUpload to upload image

import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';

buildHeadersDeals(){
    this.header = new Headers();
    this.header.append('Authorization', 'Basic ' 
                                      +btoa("test:test"));
}

uploadPhoto(image, token) {
    this.buildHeadersDeals();

    url = 'http://192.168.2.12/api/upload?token="+token;

    const fileTransfer: FileTransferObject = this.transfer.create();
    let options: FileUploadOptions = {
             fileKey: 'photo',
             fileName: image.substr(image.lastIndexOf('/')+1),
             chunkedMode: true,
             mimeType: "image/jpeg",
             headers: this.header,
          }

    return fileTransfer.upload(image, encodeURI(url), options)
                .then((data) => {
                       console.log(data);
                       return data;
           }, (err) => {
               console.log(err);
         });
   }

AND my api end is:

public function upload(Request $request) {
     if ($request->hasFile('photo')) {
      $image = $request->file('photo');
      $response['image'] = $image;
      return response()->json($response,200);
    } 
}

I have two problems:

1) I always get photo as null ($request->file('photo'))

2) Can some one tell me to send token as params, the below code does not work:

let options: FileUploadOptions = {
             fileKey: 'photo',
             fileName: image.substr(image.lastIndexOf('/')+1),
             chunkedMode: true,
             mimeType: "image/jpeg",
             headers: this.header,
             params: {
                'token': 'sffsdhnzchvh'
             }
          }

Thanks


回答1:


i have install "cordova-plugin-camera" : "^4.0.2" and "cordova-plugin-file": "^6.0.1"

fuction call here -> this.selectImage(this.camera.PictureSourceType.CAMERA);

selectImage(selection: any) {
var options: any;

options = {
  quality: 75,
  destinationType: this.camera.DestinationType.DATA_URL,
  sourceType: selection,
  allowEdit: true,
  encodingType: this.camera.EncodingType.JPEG,     
  saveToPhotoAlbum: false
};

this.camera.getPicture(options).then((imgUrl) => {

  if (options.destinationType == this.camera.DestinationType.FILE_URI) {
    console.log(imgUrl,'if');
    var sourceDirectory = imgUrl.substring(0, imgUrl.lastIndexOf('/') + 1);
    var sourceFileName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1, imgUrl.length);
    sourceFileName = sourceFileName.split('?').shift();
    this.File.copyFile(sourceDirectory, sourceFileName, cordova.file.externalApplicationStorageDirectory, sourceFileName).then((result: any) => {
      this.imageNewPath = result.nativeURL;

      // do api call here

    }, (err) => {
      console.log(JSON.stringify(err));
    })
  }
  else {
    console.log(imgUrl,'else');
    this.imageNewPath = "data:image/jpeg;base64," + imgUrl;
    //do here 
  }
}, (err) => {
  console.log("Error in choosing image :" + JSON.stringify(err));
});

}


来源:https://stackoverflow.com/questions/47491623/upload-image-ionic-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!