0

I would like to load an excel file to send it with axios to Controller and Maatwebsite\Excel for an Import.

The import part in Controller is working when i use Php from blade, i have a problem when sending from my Vuejs Component. I can't Read the Excel File. or Maybe i can't read it in Controller.

This is my code :

<template>
  <input type="file"  @change="checkFile" />
  <button @click="importExcel()">Import File</button>
</template>

<script>
//In method 
  checkFile(e) {
    var files = e.target.files || e.dataTransfer.files;
    console.log('#', files); // The file is in console
    if (!files.length)
    return;
    this.createFile(files[0]);
  },

createFile(file) {
  var reader = new FileReader();
  var vm = this;
  reader.readAsDataURL(file)
  vm.ex.excel=file; // my ex.excel object contain File
 },

importExcel: function () {
    var formData = new FormData();
    formData.append("file", this.ex.excel);
    axios.post('/importExcel', formData)
          },  

</script>

So in Controller, i use this code when i'm using php (working)

public function importExcel(Request $request)
{
    if($request->hasFile('import_file')){
       Excel::import(new UsersImport, request()->file('import_file'));
    }
    return back();
}

When i try this code for axios. i have an error :

public function importExcel(Request $request)
{
    Excel::import(new UsersImport, $request->excel);
    return back();
}

Error: No ReaderType or WriterType could be detected

Console.log(file) in imageenter image description here

UPDATE: In controller i added

$a = $request->excel;
dd($a);
result in : null

2 Answers 2

2

<template>
  <input type="file" ref="file"  @change="checkFile" />
  <button @click="importExcel()">Import File</button>
</template>

<script>
//In method 
{
  ...

  createFile(file) {
    this.ex.excel = this.$refs.file.target.value.files[0]
  }
  ...
  }
</script>

<?php

...
public function importExcel(Request $request)
{
    Excel::import(new UsersImport, $request->file('file'));
    return back();
}
...

Sign up to request clarification or add additional context in comments.

Comments

0

looks like the mime-type is missing, try add the mime-type together with your HTTP POST ...

1 Comment

Thank you for your response. I don't know how to do that. Any explications please ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.