Laravel 4 TokenMissMatchException与Plupload

艾伦

我正在尝试使用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">
爱国者

如果通过ajax完成上传,则您的csrf过滤器期望令牌位于“ x-csrf-token”标头中,而不是输入中。

而不是将令牌添加到中multipart_params,请尝试将其添加到中headers

$("#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,
    headers: {
        "x-csrf-token" : $("[name=_token]").val()
    }
});

编辑

除了上述更改之外,还确定javascript没找到_token输入元素来获取值。该问题的解决方案是在CSS属性选择器中的值周围添加引号。

最终运行的javascript:

$("input[name='_token']").val()

有关属性选择器的CSS3文档可在此处找到尽管某些浏览器在不带引号的情况下工作,但它们提供的示例显示了被引用的选择器值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章