2013-10-08 92 views

回答

2

客戶端(JavaScript代碼): 我花了一段時間來弄清楚如何在使用服務器上laravel 4使用的文件上傳功能在PhoneGap的。這是其他人誰可能會尋找一些幫助:

確保:

  • 公共/圖像(或您要上傳FIE文件夾是可寫的,否則你會得到錯誤代碼的任何其他目標文件夾:1個
  • options.fileKey的值必須匹配Input::file(options.fileKey)->move()
  • 您發表的帖子請求

$( '上傳 ')。在(' 點擊',()的函數 {

   navigator.camera.getPicture(cameraSuccess,cameraFail, 
       { 
        quality: 50, 
        destinationType: Camera.DestinationType.FILE_URI, 
        mediaType: Camera.MediaType.PICTURE, 
        sourceType : Camera.PictureSourceType.PHOTOLIBRARY }); 

     }); 

    function cameraSuccess(imageURI) 
      { 

       //set file upload options 
       var options = new FileUploadOptions(); 

        options.fileKey="file"; 
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); 
        options.mimeType="image/jpeg"; 
        options.chunkedMode = false; 


        var ft = new FileTransfer(); 

        ft.upload(imageURI, encodeURI(url+'/file-upload'), win, fail, options); 


      function win(r) { 
       console.log("Code = " + r.responseCode); 
       console.log("Response = " + r.response); 
       console.log("Sent = " + r.bytesSent); 
      } 

      function fail(error) { 

       console.log("An error has occurred: Code = " + error.code); 
       console.log("upload error source " + error.source); 
       console.log("upload error target " + error.target); 
      } 

服務器端(Laravel)代碼

Route::post('/file-upload',function() 
{ 
    //I am storing the image in the public/images folder 
    $destinationPath = 'images/'; 

    $newImageName='MyImage.jpg'; 

    //Rename and move the file to the destination folder 
    Input::file('file')->move($destinationPath,$newImageName); 

} 

也就是說所有這一切都需要在服務器端。成功上傳成功回調函數win將運行。