获取Uploadify以使用asp.net-mvc

时间:2010-09-24 23:27:45

标签: asp.net-mvc file-upload uploadify

我正在尝试让Uploadify与我的网站一起工作但是我甚至在将文件发送到服务器之前就得到了一个通用的“HTTP错误”(我说这是因为Fiddler没有向我的控制器显示任何发布请求。< / p>

我可以正确浏览要上传的文件。队列正确填充了要上传的文件,但是当我点击提交按钮时,队列中的元素会变成红色并说出HTTP错误。

无论如何这是我的部分代码:

<% using ( Html.BeginForm( "Upload", "Document", FormMethod.Post, new { enctype = "multipart/form-data" } ) ) { %>
<link type="text/css" rel="Stylesheet" media="screen" href="/_assets/css/uploadify/uploadify.css" />
<script type="text/javascript" src="/_assets/js/uploadify/swfobject.js"></script>
<script type="text/javascript" src="/_assets/js/uploadify/jquery.uploadify.v2.1.0.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {

        $("[ID$=uploadTabs]").tabs();

        var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
        $('#fileInput').uploadify({
            uploader: '/_assets/swf/uploadify.swf',
            script: '/Document/Upload',
            folder: '/_uploads',
            cancelImg: '/_assets/images/cancel.png',
            auto: false,
            multi: false,
            scriptData: { token: auth },
            fileDesc: 'Any document type',
            fileExt: '*.doc;*.docx;*.xls;*.xlsx;*.pdf',
            sizeLimit: 5000000,
            scriptAccess: 'always', //testing locally. comment before deploy
            buttonText: 'Browse...'
        });

        $("#btnSave").button().click(function(event) {
            event.preventDefault();
            $('#fileInput').uploadifyUpload();
        });

    });
</script>
    <div id="uploadTabs">
        <ul>
            <li><a href="#u-tabs-1">Upload file</a></li>
        </ul>
        <div id="u-tabs-1">
            <div>
            <input id="fileInput" name="fileInput" type="file" />
            </div>
            <div style="text-align:right;padding:20px 0px 0px 0px;">
                <input type="submit" id="btnSave" value="Upload file" />
            </div>
        </div>
    </div>
<% } %>

非常感谢您的帮助!

更新

我在uploadify脚本中添加了一个“onError”处理程序,以探索发生了哪个错误,如下例所示

onError: function(event, queueID, fileObj, errorObj) {
    alert("Error!!! Type: [" + errorObj.type + "] Info [" + errorObj.info + "]");
}

并发现info属性包含 302 。我还添加了“method”参数来uploadify,其值为'post'

我包含我的控制器操作代码以供参考。我已经阅读了很多关于uloadify的帖子,似乎我可以使用具有以下签名的动作......

[HttpPost]
public ActionResult Upload(string token, HttpPostedFileBase fileData) {
    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
    if (ticket!=null) {
        var identity = new FormsIdentity(ticket);
        if(identity.IsAuthenticated) {
            try {
                //Save file and other code removed
                return Content( "File uploaded successfully!" );
            }
            catch ( Exception ex ) {
                return Content( "Error uploading file: " + ex.Message );
            }
        }
    }
    throw new InvalidOperationException("The user is not authenticated.");
}

有人可以提供一些帮助吗?

1 个答案:

答案 0 :(得分:7)

做得好,问题消失了!

我的代码没有“正确”问题。插件的使用通常是正确的,但是身份验证机制存在问题。

由于每个人都可以在互联网上找到flash插件不与服务器端代码共享身份验证cookie,这就是在我的代码中使用包含Authentication Cookie的“scriptData”部分的原因。

问题与控制器使用[Authorize]属性进行修饰这一事实有关,而且永远不会让请求到达目的地。

在uploadify论坛上另一位用户的帮助下找到的解决方案是编写AuthorizeAttribute的自定义版本,如下面的代码所示。

/// <summary>
/// A custom version of the <see cref="AuthorizeAttribute"/> that supports working
/// around a cookie/session bug in Flash.  
/// </summary>
/// <remarks>
/// Details of the bug and workaround can be found on this blog:
/// http://geekswithblogs.net/apopovsky/archive/2009/05/06/working-around-flash-cookie-bug-in-asp.net-mvc.aspx
/// </remarks>
[AttributeUsage( AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true )]
public class TokenizedAuthorizeAttribute : AuthorizeAttribute
{
    /// <summary>
    /// The key to the authentication token that should be submitted somewhere in the request.
    /// </summary>
    private const string TOKEN_KEY = "AuthenticationToken";

    /// <summary>
    /// This changes the behavior of AuthorizeCore so that it will only authorize
    /// users if a valid token is submitted with the request.
    /// </summary>
    /// <param name="httpContext"></param>
    /// <returns></returns>
    protected override bool AuthorizeCore( System.Web.HttpContextBase httpContext ) {
        string token = httpContext.Request.Params[TOKEN_KEY];

        if ( token != null ) {
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt( token );

            if ( ticket != null ) {
                FormsIdentity identity = new FormsIdentity( ticket );
                string[] roles = System.Web.Security.Roles.GetRolesForUser( identity.Name );
                GenericPrincipal principal = new GenericPrincipal( identity, roles );
                httpContext.User = principal;
            }
        }

        return base.AuthorizeCore( httpContext );
    }
}

使用它来装饰执行上传的控制器/动作,使一切顺利进行。

唯一仍未解决的奇怪的事情,但不影响代码的执行,奇怪的是,Fiddler没有显示HTTP帖子。我不明白为什么......

我发布此信息以便社区使用。

谢谢!