dimanche 4 juin 2017

Laravel Upload File Error Method Not Allowed

I am using this plugin in laravel for uploading file to external server using ftp

kartik-v/bootstrap-fileinput

I am using ajax upload to upload file to server but i am getting laravel error of method not allowed , I know this error usually caused because of GET & POST confusion in routes but I didn't find any solution for this.

My File Upload Form :

enter image description here

HTML Form :

{!! Form::open(array('url' => 'uploadfile', 'method' => 'post','class'=> 'form')) !!}   
<div class="form-group">
<input id="file-4" type="file" class="file" data-upload-url="#" name="file[]">
</div>


<div class="form-group">
<label for="type">Document Type</label>
<input type="text" name="type" id="type" class="form-control">
</div>

<div class="form-group">
<label for="description">Description</label>
<textarea name="description" id="description" class="form-control"></textarea>
</div>

<input type="hidden" name="ClientId" value="">

{!! Form::close() !!}

Routes :

Route::post('uploadfile','CdnController@UploadFile');
Route::get('account/client-profile/{id}','AccountController@ClientProfileV2');

Get Route will display the Upload New Document Form Post Route has logic to upload file to ftp server

Jquery Code :

<script>

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

$(document).ready(function () {
    $("#file").fileinput({
        uploadAsync: false,
        uploadUrl: "uploadfile", 
        uploadExtraData: function() {
        return {
               type: $("#type").val(),
               description:$("#description").val(),
               clientId:$('#clientId').val()
              };
        }
    });
});

</script>

Php code to upload file to external server:

public function UploadFile(Request $request)
    {

        if(Storage::directories($request->ClientId)==NULL)
            Storage::makeDirectory('documents');

        $file = $request->file('file'); 

        Storage::disk('ftp')->put($request->ClientId.'/'.
            $file->getClientOriginalName(),
            file_get_contents($file->getRealPath())
        );

        return json_encode(['message'=>'Files were uploaded.']);
    }

When I try to upload file , then i get error message

enter image description here

enter image description here

I am confused here , as action in form is route 'uploadfile' but in headers i see requested url is the url from where i am uploading new document. And also there is error message 405 Method Now Allowed.

It's confusing how is the exact flow of this plugin , it should go through the UploadFile() but that isn't going there and returning error message as 405 Method Not Allowed.

Help would be appreciated.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2rFwk0C
via IFTTT

Aucun commentaire:

Enregistrer un commentaire