简体   繁体   English

dropzone laravel,显示419错误

[英]Dropzone with laravel, it displays 419 error

I have tested all that I have seen but it has not worked to me, I am using dropzone with laravel and my problem is that it displays a 419 error, I know what it means it's about csrf token but I can not fix it.我已经测试了我所看到的所有内容,但它对我不起作用,我正在使用带有 laravel 的 dropzone,我的问题是它显示 419 错误,我知道这与 csrf 令牌有关,但我无法修复它。

My HTML code is this:我的 HTML 代码是这样的:

<div id="my-dropzone" class="dropzone"></div>

My javascript code is this:我的 javascript 代码是这样的:

Dropzone.autoDiscover = false;

$(document).ready(function(){

   $('#my-dropzone').dropzone({
      url: 'http://54.161.128.196/pre_image/store',
      headers: {
        'X-CSRF-TOKEN': $('meta[name="token"]').attr('content')
      },
      method: 'post',
      dictResponseError: 'Error uploading file!'
   });

});

My Laravel route:我的 Laravel 路线:

Route::post('/pre_image/store', 'Pre_ImageController@store');

I put this in the middleware to see if I could remove the 419 error:我将它放在中间件中以查看是否可以删除 419 错误:

It is in the VerifyCsrfToken middleware它在 VerifyCsrfToken 中间件中

protected $except = [
    'pre_image/store'
];

And it did not work, I have the csrf token in the meta like this:它没有用,我在元中有这样的 csrf 令牌:

<meta name="csrf-token" content="{{ csrf_token() }}">

This is screenshot of the error:这是错误的屏幕截图:

截屏

So I wonder what is the problem why does it display 419 error if it that route is disabled to verify the token and even it has the token assigned?所以我想知道问题是什么,如果禁用该路由来验证令牌,甚至它已分配令牌,为什么它会显示 419 错误?

Thanks!谢谢!

I had similar issues with Dropzone.我对 Dropzone 也有类似的问题。 I ended up pulling the headers into an ajax function at the top of the page rather than in the DZ headers section.我最终将标题拉入页面顶部的 ajax function 而不是 DZ 标题部分。 This allowed for any general ajax request to have the correct CSRF.这允许任何通用 ajax 请求具有正确的 CSRF。

Try this:尝试这个:

$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

If anybody else is getting this problem uploading images in a form, and the above solution doesn't work, here is my solution.如果其他人在以表格形式上传图片时遇到此问题,并且上述解决方案不起作用,那么这是我的解决方案。 I found the Javascript code that was generating the src data on the webpage.我在网页上找到了生成 src 数据的 Javascript 代码。

In my case, it was this:就我而言,是这样的:

  $('#'+newId).attr('src', canvas.toDataURL());

The problem with this is that it defaults to a PNG file, which is very large.这个问题是它默认为一个非常大的 PNG 文件。 If you change it to the following, you will have a much smaller file which will be a lot easier to upload.如果将其更改为以下内容,您将拥有一个更小的文件,上传起来会容易得多。

$('#'+newId).attr('src', canvas.toDataURL('image/jpeg', 0.7));

This will produce a much smaller upload file.这将产生一个小得多的上传文件。 My problem was if I tried to upload more than 3 pictures I would get the error message.我的问题是,如果我尝试上传超过 3 张图片,我会收到错误消息。 If the quality isn't good enough, change the 0.7 to 0.8.如果质量不够好,请将 0.7 更改为 0.8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM