【发布时间】:2021-12-18 10:37:47
【问题描述】:
我尝试制作一个页面来将一些文件上传到服务器端。但它不起作用。
// **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;
}
}
}
这些代码返回 500 内部服务器错误。但如果我返回 $file,它会返回 javascript 文件对象。我不知道为什么我不能保存上传的文件。 getClientOriginalName 也返回 500 内部服务器错误。最后,try-catch 也返回 500 internal server error。
感谢您的帮助...
【问题讨论】:
-
一个 500 错误可能是很多事情,分享你在错误日志中发现的实际错误。为什么不使用 laravel 文件功能上传文件呢?
-
请记录您的请求,以便查看文件是否已发送。因为您的请求应该是 multipart/form-data 请求,我猜是因为您错过了没有发送任何文件...
-
您需要在
request()->file('input_name_attribute_here')中添加文件字段的名称。 -
在这种情况下
move是什么? -
@Wtow 我认为它是“文件”,但您可以随时转储
request()->all()以找出它是什么。
标签: php laravel dropzone.js