首页 > 解决方案 > 将 Vuejs 中的 excel 文件发布到 Laravel

问题描述

我想将本地驱动器上的 Excel 文件发布到 Laravel 应用程序中。但是我收到下面的错误消息,我不知道我是没有通过 axios 正确发送文件还是没有在 Laravel 端接收它。

这是我的 Javascript 文件:

// import { LocalStorage } from 'quasar'
import Pusher from 'pusher-js'
import { PythonShell } from 'python-shell'
import axios from 'axios'
const FormData = require('form-data')
const fs = require('fs')
const path = require('path')

const options = {
  // mode: 'text',
  // pythonPath: 'path/to/python',
  // pythonOptions: ['-u'], // get print results in real-time
  scriptPath: path.join(__dirname, '/../rpa')
  // args: ['value1', 'value2', 'value3']
}

const exportDir = 'C:\\TEMP\\TFMCH\\'

export const rpa = {
tfmch_LX02 (warehouse, filePath, truncate) {
    console.log('Updating LX02 on TFMCH')
    const URL = 'http://localhost/tfmch_LARAVEL/public/api/' + 'rpa/lx02'
    const formData = new FormData()
    formData.append('lx02', fs.createReadStream(filePath))
    formData.append('truncate', truncate)
    const formConfig = {
      headers: { 'Content-Type': 'multipart/form-data' }
    }
    console.log(URL)
    console.log(fs.createReadStream(filePath))
    // console.log(filePath)
    // console.log(formData)
    // console.log(formConfig)

    axios.post(URL, formData, formConfig)
      .then((response) => {
        console.log(response.data)
        // if (warehouse === 805) {
        //   rpa.sap_LX02_wh(804, false)
        // }
      })
      .catch((e) => {
        console.log(e)
        // console.log('error')
      })
  }
}

这是我的 Laravel API:

Route::post('rpa/lx02', function(Request $request){

ini_set('max_execution_time', 300);

// return response();

if ($request->truncate) {
    LX02::query()->truncate();
}

$contents = file_get_contents($request->lx02->path());
// return response($contents);

Excel::import(new LX02Import, $contents);

$config = new Upload;
$config->table_name = 'LX02';
$config->save();

// $deletedRows = lx02::where('sloc', 'LIKE', 'S%')->delete();

return response()->json("Updated"); 

});

我从 Laravel 收到错误消息:

{
"message": "Call to a member function path() on string",
"exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
"file": "C:\\Users\\Ingo\\Google Drive\\1 - Webapps\\tfmch_LARAVEL\\routes\\api.php",
"line": 458,
"trace": [

标签: laravelvuejs2axios

解决方案


推荐阅读