laravel - 无法让 Dropzone 与 Laravel 一起工作
问题描述
使用 Laravel 框架 6.18.0
我确定这很简单,但我似乎无法弄清楚。我没有看到控制器中的函数被调用来实际完成将文件移动到服务器的工作。我已经附上了下图的链接,因为 dropzone 似乎正在完成它的工作,但路由/控制器端似乎失败了。
我的路线:
Route::get('/uploadpics', 'UploadPicController@index');
Route::post('/upload','UploadPicController@uploadFiles');
上传图片.blade.php:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="{{asset('js/plugins/dropzone/dist/min/dropzone.min.css')}}">
<script src="{{asset('js/plugins/dropzone/dropzone.min.js')}}" type="text/javascript"></script>
</head>
<body>
<div class="content">
<form method="post" action="{{url('/upload')}}" enctype="multipart/form-data" class="dropzone" id="dropzone">
@csrf
</form>
</div>
<script>
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone(".dropzone",{
maxFilesize: 3, // 3 mb
acceptedFiles: ".jpeg,.jpg,.png",
});
</script>
</body>
</html>
我的 UploadPicController.php
<?php
namespace App\Http\Controllers;
use App\Coin;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Carbon\Carbon;
class UploadPicController extends Controller
{
public function index(){
return view('pages.uploadpics');
}
public function uploadFiles(Request $request){
dd("i'm in upload"); //**I never get here...**
if($request->hasFile('file')) {
// Upload path
$destinationPath = 'images/';
// Create directory if not exists
if (!file_exists($destinationPath)) {
mkdir($destinationPath, 0755, true);
}
// Get file extension
$extension = $request->file('file')->getClientOriginalExtension();
// Valid extensions
$validextensions = array("jpeg","jpg","png");
// Check extension
if(in_array(strtolower($extension), $validextensions)){
// Rename file
$fileName = str_slug(Carbon::now()->toDayDateTimeString()).rand(11111, 99999) .'.' . $extension;
// Uploading file to given path
$request->file('file')->move($destinationPath, $fileName);
}
}
}
}
我可以在页面上使用 dropzone 而不会出现错误,但它不会将文件复制到服务器 - 这就像控制器中的函数永远不会被调用(参见 dd(); - 从不命中)。
解决方案
推荐阅读
- python - 在 Flask 中向我自己的本地主机发出 POST 请求后出现 WinError 10061
- excel - Excel 高级过滤器显示相同的值
- python - py_call_impl 和 com_error 中的错误:用于 reticulate 的 RPC
- webhooks - 如何在工单的受让人更新/更改时触发 webhook
- rust - 如何匹配宏中的元组
- python - Python Selenium 无法定位元素?
- apache-kafka - Kafka Topic 分区重新分配时,Flink 作业连续失败
- c# - 在 asp.net mvc 中打印有效的字符串数组
- shopify - 将可拖动的 png 添加到主页
- openwrt - 如何将dnp3包安装到openwrt?