【问题标题】:Phonegap + Laravel 4 How to upload filePhonegap + Laravel 4 如何上传文件
【发布时间】:2013-10-09 22:52:48
【问题描述】:

我自己回答了这个问题,这样其他人就不会遇到我遇到的问题。

【问题讨论】:

    标签: cordova laravel laravel-4 phonegap-plugins


    【解决方案1】:

    客户端(Javascript 代码): 我花了一段时间才弄清楚如何在服务器上使用 laravel 4 时在 phonegap 中使用文件上传功能。这适用于可能正在寻求帮助的其他人:

    确保:

    • public/images(或您希望上传的任何其他目标文件夹 fie 文件夹是可写的,否则您将收到 错误代码:1
    • options.fileKey 的值必须匹配Input::file(options.fileKey)->move()
    • 您提出了一个发布请求。

    $('.upload').on('点击',function() {

                  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会运行。

    【讨论】:

      猜你喜欢
      • 2013-07-22
      • 2013-04-06
      • 2015-03-21
      • 2013-08-11
      • 2014-03-07
      • 2015-04-24
      • 1970-01-01
      • 2013-03-14
      • 2013-08-14
      相关资源
      最近更新 更多