【问题标题】:$request->file keeps returning tmp path$request->file 不断返回 tmp 路径
【发布时间】:2020-11-19 02:08:30
【问题描述】:

我正在尝试使用 laravel 和 vue 上传文件。当我 console.log() 文件时,我得到下面的图片,但在控制器中,我只收到一个 tmp 路径。

Vue

onFileChange(e) {
   ths.file = e.target.files[0];
   console.log(this.file)
},
submit(){
   let form= new FormData()
   form.append('file', this.file)
   axios.post('/api/archieve',form).then(res=>{
            //
   })
},

控制器

return $request->file //returns "C:\xampp\tmp\php5E67.tmp"

更新 我已经检查过 dd($request->file('file'));它返回下面的图像,但 realPath 是错误的。图像存储在我电脑上的不同文件夹中。

【问题讨论】:

  • 检查内部$request->file()
  • 如果您的表单中有content-type = "multipart/form-data",请删除它,因为这个答案是说它给他带来了问题stackoverflow.com/a/43584379/1545904
  • @V-K 返回空文件。
  • @zizoujab 不,我没有导致 FormData() 默认已经包含
  • @V-K 对不起,它返回相同的东西 C:\xampp\tmp\php8F5A.tmp

标签: php laravel vue.js


【解决方案1】:

请尝试添加标题。

  onFileChange(e) {
    this.file = e.target.files[0];
    console.log(this.file)
  },
  submit() {
    let form= new FormData()
    form.append('file', this.file)
    axios.post('/api/archieve', form,  {
      headers: { 'Content-Type': 'multipart/form-data' }
    }).then(res=>{
      //
    })
  },

在你的控制器中,检查

$request->file('file');

存储文件

use Illuminate\Support\Facades\Storage;
....

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

$path = 'my-uploads'; // path or folder where to save it
$storePath = Storage::put($path, $file);
$fileName = basename($storePath);  // generated file name

$filePath = $path . '/' . $fileName; // path of file in your storage

【讨论】:

  • 还是一样。仅获取 tmp 路径
  • 是的,它会返回路径。尝试dd($request->file('file')); 并检查控制台网络
  • 就是这样!现在您可以存储文件$storePath = Storage::put($pathInStorage, $file);
  • 但是为什么realPath是错误的,图片在另一个文件夹中
猜你喜欢
  • 2016-11-08
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 2021-08-12
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多