【发布时间】:2015-03-11 21:53:09
【问题描述】:
我正在尝试使用 Plupload 上传文件,但我总是收到 TokenMissMatchException。
// Route
Route::post("/posts/gallery", "PostsController@uploadGallery");
// Controller action
public function uploadGallery(){
$file = Input::file('file');
$destinationPath = public_path() . '/imgs';
$extension = $file->getClientOriginalExtension();
$filename = "post-" . str_random(12) . "." . $extension;
$inFile = $file->getRealPath();
$outFile = public_path() . "/imgs/" . $filename;
$image = new Imagick($inFile);
$image->thumbnailImage(550, 0);
if($image->writeImage($outFile)){
return Response::json(["response" => "ok", "img" => $filename]);
}else{
return Response::json(["response" => "error"]);
}
}
这是我尝试解决的问题。我尝试将_token 添加到请求中,但没有收到:
$("#uploader").pluploadQueue({
runtimes : 'html5,flash,silverlight,html4',
url : "{{ URL::action('PostsController@uploadGallery') }}",
chunk_size: '1mb',
rename : true,
dragdrop: true,
filters : {
max_file_size : '3mb',
mime_types: [
{title : "Image files", extensions : "jpg,gif,png"},
]
},
resize : {width : 320, height : 240, quality : 90},
flash_swf_url : "<?php echo public_path() . '/js/Moxie.swf'; ?>",
silverlight_xap_url : "<?php echo public_path() . '/js/Moxie.xap'; ?>",
prevent_duplicates: true,
multipart_params : {
"_token" : $("[name=_token]").val()
}
});
在 filters.php 我有这个:
Route::filter('csrf', function() {
$token = Request::ajax() ? Request::header('x-csrf-token') : Input::get('_token');
if (Session::token() != $token){
throw new Illuminate\Session\TokenMismatchException;
}
});
有人可以帮我解决这个问题吗?
更新:
上传文件的html表单:
<div class="imageGallery">
{{ Form::open() }}
<div id="uploader">
<p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
</div>
{{ Form::close() }}
</div>
//隐藏输入框
<input type="hidden" value="VJRUpvq92oYxCsNHVBi5TqqkU6I6CQayay6x7L0m" name="_token">
【问题讨论】:
-
console.log($("input[name=_token]").val())是否返回任何内容?我只问因为选择器对我来说看起来很奇怪(但我可能是在想象事情)。 -
不,它不会返回任何东西。我也尝试在 Firebug 控制台中执行 $("input[name=_token]").val() 但仍然没有
-
嗯。现在这真的很奇怪......我唯一能想到的就是你遇到了一些其他错误,这些错误会阻止任何其他 JS 运行。加载页面/发出请求时控制台中有任何内容吗?
-
$("[name='_token']")或$("input[name='_token']")怎么样。您是否查看过页面的来源以确保输入确实存在?确保它不是您忘记包含的部分,或者某处没有某些 javascript 由于某种原因正在删除该元素。 -
$("input[name='_token']") 正在工作。谢谢@patricus
标签: php laravel laravel-4 csrf plupload