Laravel 文件未上传

Laravel file is not uploded

我试着做一个页面来上传一些文件到服务器端。但是没用。

// **Here my upload.blade.php**

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <title>Laravel Multiple Files Upload Using Dropzone with - CodingDriver</title>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.7.0/min/dropzone.min.css">

</head>
  <style>
  .alert-message {
    color: red;
  }
</style>
<body>

<div class="container">
    <h2 style="margin-top: 12px;" class="alert alert-success">Laravel Multiple Files Upload Using Dropzone -
       <a href="https://www.codingdriver.com" target="_blank" >CodingDriver</a>
     </h2>
    <div class="row" style="clear: both;margin-top: 18px;">
        <div class="col-12">
          <div class="dropzone" id="file-dropzone"></div>
        </div>
    </div>
</div>
</body>
</html>

<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.7.0/min/dropzone.min.js"></script>

<script>
  Dropzone.options.fileDropzone = {
    url: 'upload/classification',
    acceptedFiles: ".jpeg,.jpg,.png,.gif",
    addRemoveLinks: true,
    maxFilesize: 8,
    headers: {
    'X-CSRF-TOKEN': "{{ csrf_token() }}"
    },
    
    removedfile: function(file)
    {
      var name = file.upload.filename;
      $.ajax({
        type: 'POST',
        url: 'file.remove',
        data: { "_token": "{{ csrf_token() }}", name: name},
        success: function (data){
            console.log("File has been successfully removed!!");
        },
        error: function(e) {
            console.log(e);
        }});
        var fileRef;
        return (fileRef = file.previewElement) != null ?
        fileRef.parentNode.removeChild(file.previewElement) : void 0;
    },
    success: function (file, response) {
      console.log(response);
    },
  }
</script>
// **my web.php**


Route::get('upload',function(){
    return view('upload');
});
Route::post('upload/classification', [imageClassificationController::class, 'uploadDataset']);
<?php

// my imageClassificationController.php

namespace App\Http\Controllers;
use App\Models\imageClassificationModel;
use Illuminate\Http\Request;
use App\Models\fileTransferModel;
use Auth;
config('projectConfigs.pathConfigs');
class imageClassificationController extends Controller
{
    
    public function uploadDataset()
    {
        try{
        
        $file = request()->file();
        //echo 'File Name: '.$file->getClientOriginalName();
        //return __DIR__;
        //$fileName= $file->getClientOriginalName();
        //return $file;
        

$file->move(__USERFOLDERS__.DIRECTORY_SEPARATOR.Auth::user('foldername').DIRECTORY_SEPARATOR.'image-classification'.DIRECTORY_SEPARATOR.'datasets',$file);
        return $file->getClientOriginalName();
    }
        catch(Exception $e){
            return 'test'.$e;
            
        }
        
    }
    
    

}

这些代码return 500 内部服务器错误。但是如果我 return $file,它 returns javascript 文件对象。我不知道为什么我不能保存上传的文件。还有 getClientOriginalName returns 500 内部服务器错误。最后,try-catch 也 returns 500 内部服务器错误。

感谢您的帮助...

您必须将输入名称添加到请求文件功能,并使用 Laravel 文件移动功能:

public function uploadDataset()
{
    try{
    
    $file = request()->file('file'); // the input name attribute
    

    $file->move(__USERFOLDERS__.DIRECTORY_SEPARATOR.Auth::user('foldername').DIRECTORY_SEPARATOR.'image-classification'.DIRECTORY_SEPARATOR.'datasets'.DIRECTORY_SEPARATOR, $file->getClientOriginalName());
    return $file->getClientOriginalName();
    }
    catch(Exception $e){
        return 'test'.$e;
        
    }
    
}