【问题标题】:Laravel Dropzone.js some photos cannot be saved into the folderLaravel Dropzone.js 一些照片无法保存到文件夹中
【发布时间】:2018-01-01 22:30:01
【问题描述】:

我正在使用 dropzone.js 来允许用户将多张照片上传到数据库。在我为图像上传功能设置数据库之前,我想看看是否可以将图像保存到我想要的文件夹中。但是,在成功上传了 3 张照片后(由于在上传图片后立即显示了勾号,我认为图片已成功上传),其中只有 2 张出现在文件夹中。

在以下代码中,我列出了视图(包括 javascript 和 html 代码)、路由和控制器文件:

CSS ->

{{html::style('https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.css')}}

HTML ->

{!! Form::open(array('route' => 'gallery.images.upload', 'class' => 'dropzone', 'enctype' => 'multipart/form-data','files' => true, 'id' => 'addImages', 'method' => "POST")) !!}

  {{ Form::hidden('gallery_id', $gallery->id) }}

{!! Form::close() !!}

Javascript ->

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.js"></script>

<script type="text/javascript">
    Dropzone.options.addImages = {
        maxFilesize: 5,
        acceptedFiles: 'image/jpeg,image/png,image/gif',
        paramName: "file",
    }
</script>

路线 ->

Route::prefix('gallery')->group(function(){
    Route::post('/images/upload', 'GalleryController@uploadGalleryImages')->name('gallery.images.upload');
});

控制器 ->

public function uploadGalleryImages(Request $request)
{

    if ($request->hasFile('file')) {

        // Get the file from the file request
        $file = $request->file('file');

        // set my file name
        $originalName = $file->getClientOriginalName();

        $filename = uniqid() . $originalName;

        $file->move('galleries/images/', $filename);

    } 
    else{
        return redirect()->route('gallery.view');
    }
}

我已经问了好几个星期了,但没有得到任何答案。我真的需要你的帮助!

Screenshot of images being successfully uploaded

Screenshot of images present in the folder

【问题讨论】:

  • 控制台中有什么?
  • @AndyHolmes 在控制台中它向我展示了这个:[Violation] 'load' handler took 151ms dropzone.min.js:1
  • @AndyHomes 有时会显示:[Violation] Forced reflow while executing JavaScript took 34ms

标签: laravel dropzone.js


【解决方案1】:

这发生在我身上一次,因为服务器在浏览图像时会覆盖一些图像,因为它们具有相同的文件名,为避免这种情况,请使用类似的方法来确保您拥有唯一的文件名:

$random = mt_rand(1,1000);
$filename  = time().''.$random. '.' . $file->getClientOriginalExtension();

【讨论】:

  • 你看到$filename = uniqid() . $originalName;了吗?
  • 是的,可能是uniqueid()函数的问题,尝试用随机值添加时间,以确保其唯一性。
  • @Sletheren 感谢您的回复。当我将 uniqid() 函数更改为您评论中给出的行时,问题仍然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 2021-06-03
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 2020-05-20
相关资源
最近更新 更多