在表单提交中使用plupload时未收到AntiForgeryToken

时间:2011-07-23 08:12:19

标签: asp.net-mvc-3 plupload antiforgerytoken

我正在使用Plupload将多个图像上传到mvc3网络应用程序。

文件上传确定,但是当我引入AntiForgeryToken时它不起作用,错误是没有提供令牌,或者它无效。

我也无法将Id参数作为动作参数接受,它总是发送null。因此必须手动从Request.UrlReferrer属性中提取它。

我认为plupload是手动提交上传中的每个文件并伪造自己的表单帖子。

我的表格......

@using (@Html.BeginForm("Upload", "Photo", new { Model.Id }, FormMethod.Post, new { id = "formUpload", enctype = "multipart/form-data" }))
 {
    @Html.AntiForgeryToken()
    @Html.HiddenFor(m => m.Id)
    <div id="uploader">
        <p>You browser doesn't have HTML5, Flash or basic file upload support, so you wont be able to upload any photos - sorry.</p>
    </div>
    <p id="status"></p>
 }

以及连接它的代码......

$(document).ready(function ()
{
    $("#uploader").plupload({
        // General settings
        runtimes: 'html5,flash,html4',
        url: '/Photo/Upload/',
        max_file_size: '8mb',
        //              chunk_size: '1mb',
        unique_names: true,

        // Resize images on clientside if we can
        resize: { width: 400, quality: 100 },

        // Specify what files to browse for
        filters: [
            { title: "Image files", extensions: "jpg,gif,png" }
        ],

        // Flash settings
        flash_swf_url: 'Content/plugins/plupload/js/plupload.flash.swf'
    });

    $("#uploader").bind('Error', function(up, err)
    {
        $('#status').append("<b>Error: " + err.code + ", Message: " + err.message + (err.file ? ", File: " + err.file.name : "") + "</b>");
    });


    // Client side form validation
    $('uploadForm').submit(function (e)
    {
        var uploader = $('#uploader').pluploadQueue();

        // Validate number of uploaded files
        if (uploader.total.uploaded == 0)
        {
            // Files in queue upload them first
            if (uploader.files.length > 0)
            {
                // When all files are uploaded submit form
                uploader.bind('UploadProgress', function ()
                {
                    if (uploader.total.uploaded == uploader.files.length)
                        $('form').submit();
                });

                uploader.start();
            } else
                alert('You must at least upload one file.');

            e.preventDefault();
        }
    });

});  

这是接收它的控制器动作......

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload(int? id, HttpPostedFileBase file)
{
    if (file.ContentLength > 0)
    {
        var parts = Request.UrlReferrer.AbsolutePath.Split('/');
        var theId = parts[parts.Length - 1];
        var fileName = theId + "_" + Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        file.SaveAs(path);
    }
    return Content("Success", "text/plain");
}

正如你所看到的,我必须使id参数可以为空,我在action方法中手动提取它。

如何确保正确发送每个表单的值?

2 个答案:

答案 0 :(得分:2)

简短回答:是的!

使用 multipart_params 选项,如下所示:

multipart_params: 
{ 
__RequestVerificationToken: $("#modal-dialog input[name=__RequestVerificationToken]").val() 
}

答案 1 :(得分:1)

简短回答:你做不到。

在这种情况下你可以做的是将你的令牌作为另一个多部分参数传递(如果使用它),或者作为GET参数作为URL的一部分传递,但是当它构建它时,plupload将不会发送表单中的任何内容自己的要求 另一种可能是使用自定义标头将令牌传递回服务器(plupload有headers选项),但无论您使用哪种方法,都必须在后端处理它以验证它。