我正在使用Dropzone使用Laravel 5上传图像。在Dropzone对我的URL进行put调用后,我收到以下错误:
VerifyCsrfToken.php第67行中的TokenMismatchException:
但是,当我查看请求的有效负载时,令牌存在:
------ WebKitFormBoundary91A7BYrMsDcGTEvx Content-Disposition:form-data;命名=" _method"
PUT ------ WebKitFormBoundary91A7BYrMsDcGTEvx Content-Disposition:form-data;名称=" _token"
j3NbjibYF7k8g2w1P0enw6YVfDrDvCGKFMCFt4NX ------ WebKitFormBoundary91A7BYrMsDcGTEvx Content-Disposition:form-data;命名="标题"
这是我的JS:
Dropzone.options.realDropzone = {
url: '/user/manage/10',
method: 'PUT',
paramName: 'file',
uploadMultiple: false,
parallelUploads: 100,
previewsContainer: '#dropzonePreview',
addRemoveLinks: true,
maxFiles: 10,
autoProcessQueue: false,
init: function () {
var dropZone = this;
this.element.querySelector("#save").addEventListener("click", function (e) {
e.preventDefault();
e.stopPropagation();
console.log("clicked submit");
dropZone.processQueue();
});
},
};
我的表格:
{!! Form::model($asset, ['method' => 'PUT', 'class' => 'dropzone', 'id' => 'real-dropzone', 'action' => ['UserManagementController@update', $asset->id], 'file' => true]) !!}
我的控制器:
public function update(Request $request, $id)
{
return dd(FileRequest::file('file'));
}
答案 0 :(得分:6)
尝试在Dropzone选项中添加令牌:
sending: function(file, xhr, formData) {
formData.append("_token", "{{ csrf_token() }}");
},
答案 1 :(得分:1)
Laravel 5.6的工作代码:
<强> HTML 强>
<div class="dropzone dropzone-previews" id="my-awesome-dropzone"></div>
<强> Jquery的强>
Dropzone.autoDiscover = false;
$(document).ready(function() {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$("div#my-awesome-dropzone").dropzone({
url: "{{url('/upload')}}",
headers: {
'x-csrf-token': CSRF_TOKEN,
},
});
});
答案 2 :(得分:0)
您只需在表单中添加{{ csrf_field() }}