【问题标题】:Dropzonejs object object error. How can I fix it?Dropzonejs 对象对象错误。我该如何解决?
【发布时间】:2021-11-03 08:58:19
【问题描述】:

文件无法上传到目录并返回错误

dropzone 中的 js 和 css 正确包含在页眉和页脚中

巴尔德代码::

@include('Templates.head')
<body style="background: #283747;">
    @include('Menu.menu')
    @if (Session::has('user') && Session::has('privlages') && Session::get('privlages') >= '1')
    <div class="container">
        <div class="row">
            <form action="/gallery_add_image" class="dropzone" style="margin-left: 10%; margin-right: 10%;">
                @csrf
                <div class="fallback">
                  <input name="file" type="file" multiple />
                </div>
            </form>
        </div>
    </div>
    @else
    <script>window.location = "/";</script>
    @endif
@include('Templates.footer')

控制器代码::

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class GalerryController extends Controller
{
    public function add_image(Request $add_image){
        $image = $add_image->file('file');
        $imageName = time(). '.' . $add_image->extension();
        $image->move(public_path('gallery'), $imageName);
        return response()->json(['success' => $imageName]);
    }
}

网络(路由)代码::

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\GalerryController;

Route::post('/gallery_add_image', [GalerryController::class, 'add_image']);

服务器响应代码::

我该如何解决这个问题?

【问题讨论】:

  • 检查网络选项卡的实际错误。
  • 我检查并添加了图片。
  • 单击红色的“gallery_add_image”行之一,然后单击PreviewResponse 以查看服务器发回的内容。
  • 您需要单击其中一条红线,然后单击响应以检查服务器响应。 prnt.sc/1rdp9n0 否则,请检查您的日志文件以了解 500 错误的性质
  • 好的,现在添加了,但我看不到有什么帮助

标签: laravel laravel-8 dropzone.js dropzone


【解决方案1】:

在您的控制器代码中$add_image 是一个请求对象,它没有extension 方法。要获取上传的文件扩展名,您需要使用:

class GalerryController extends Controller
{
    public function add_image(Request $add_image) {
        $add_image->validate([ 'file' => 'required|image' ]); // Good idea to validate
        $image = $add_image->file('file');
        $imageName = time(). '.' . $image->getClientOriginalExtension();
        $image->move(public_path('gallery'), $imageName);
        return response()->json(['success' => $imageName]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 2023-02-06
    • 2020-07-25
    • 1970-01-01
    • 2022-07-20
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多