2017-11-25 16 views
0

私はイオン3でアプリを開発しており、ルーメンフレームワークで作成されたAPIを使用してサーバーに画像をアップロードする必要があります。 画像のアップロード要求: 画像はカメラからクリックされ、base64に変換されます。アップロード画像ionic 3

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

その後、私は画像

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); 
     }); 
    } 

をアップロードするのFileUploadを使用して、私のAPIエンドがある:

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

私は2つの問題を抱えて:

1)私はいつものように写真を入手null($ request-> file( '写真'))

2)いくつかのいずれかが、以下のコードが動作しない、のparamsとしてトークンを送信するために教えてもらえます:私は「コルドバ・プラグインカメラ」をインストールしてい

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

おかげ

答えて

0

:「^ 4.0.2を」と "コルドバ・プラグインファイル": "^ 6.0.1"

のfuctionコールここ-> 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)); 
}); 

} 
関連する問題