skip to Main Content

I’m going to upload my image file to storage folder through laravel vuejs ,
but now I just tried to print the data through dd(); but it returns no response and no error how should I suppose to know if I doing correctly or not?

I think the error insists through formData when passing the data through Ajax with this

data: {form_data:formData, ........

input file

<input type='file' onchange="readURL(this);" id="file" ref="file" name="profile_avatar" accept=".png, .jpg, .jpeg"  v-model="card_image_main" enctype="multipart/form-data"/>

vuejs

module.exports = function(data) {
var _data = {
    file: '',
};
return {
    data: (() => Object.assign({}, data, _data)),
    props: {
        data_object: Object,
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            var vm = this;
            var t;
        },

    updateMain(){
      let vm = this;
      this.file = this.$refs.file.files[0];
      let formData = new FormData();
      formData.append('file', this.file);
      this.$refs.file.value='';

      $.ajax({
         url: vm.$route('staff.ajax.emvvalidationdetails.updatemain'),
         type: 'POST',
         headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'), 'undefined': 'multipart/form-data'},
         data: {form_data:formData, emvdetailsdata: vm.emvdetailsdata[0], other_ext_name: vm.other_ext_name, other_contact_name: vm.other_contact_name},
         success: function (response) {
             if (response) {
                 vm.emvdetailsdata = response;
            
                 }},
               });
                document.getElementById("EditForm").submit();
             }
       }

routes

Route::post('updatemain', 'EmvvalidationdetailsController@updatemain')->name('staff.ajax.emvvalidationdetails.updatemain');

EmvvalidationdetailsController

public function updatemain(Request $request)
{
    dd($request->all());
}

2

Answers


  1. I’d start with installing Telescope and checking the request

    Login or Signup to reply.
  2. Uploaded files are treated differently by the server than other form data. When a file is uploaded via a form, it is sent as a binary stream, not as a string, and it requires special handling to be processed properly. The $request->file() method takes care of this handling for you, whereas the $request->get() or $request->all() method does not. by updating your controller code like this you should be able to see the uploaded file and store it:

    public function updatemain(Request $request)
    {
        $file = $request->file('file');
        // dd($file);
        $file->store('public/src/');
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search