0

I have implemented Uploadify plugin on my site (site has authentication/authorization implemented). And it works fine except it display me 302 HTTP error.
I google a little and find this article but I still missing something. I suspect that error is somewhere in JS part. I saw similar questions here on stack and read them all but still have a problem :(

<script>    
    $(document).ready(function () {
        $('#file_upload').uploadify({
            'uploader': '../../Scripts/uploadify/uploadify.swf',
            'script': '../../ImageController/UploadImage',
            'cancelImg': '../../Scripts/uploadify/cancel.png',
            'scriptData': { 'token': '@Request.Cookies[FormsAuthentication.FormsCookieName].Value' },
            'folder': '/uploads',            
            'auto': false,
            'buttonText': 'Upload image',
            'displayData': 'speed',
            'multi': 'true',
            'fileExt': '*.jpg;*.gif;*.png',
            'fileDesc': 'Image Files',
            onError: function (event, queueID, fileObj, errorObj) {
                alert("Error!!! Type: [" + errorObj.type + "] Info [" + errorObj.info + "]");
            }
        });
    });
</script>

[HttpPost]
        [TokenizedAuthorizeAttribute]
        public ActionResult UploadImage(string token, string returnUrl)
        {
            //wip
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
            if (ticket != null)
            {
                var identity = new FormsIdentity(ticket);
                if (identity.IsAuthenticated)
3
  • Check the upload folder path is correct and also the path for 'script' parameter Commented Oct 16, 2011 at 12:58
  • FIle is uploaded fine. But after upload it display this error. Commented Oct 16, 2011 at 13:06
  • If you examine the 302 response using Fiddler or a similar tool, where is the request being redirected? Most likely the redirect is to your login page. Commented Oct 16, 2011 at 14:16

3 Answers 3

1

302 is redirect. I am guessing Uploadify treats anything that is not 200 as an error. Are you redirecting at the end of your UploadImage action?

Sign up to request clarification or add additional context in comments.

7 Comments

Yes in the end I have 'return RedirectToAction("Index", "Gallery");'
@1110 well that's exactly your problem. You shouldn't be redirecting from the upload target action. Instead redirect from Uploadify success handler.
But onComplete is never fired. Only onError.
@1110 yes, because you have redirect in your upload action
I have made Upload action method to be void. Now when I press upload button file is uploaded. But onComplete or onError never invoked.
|
0

In the end this was not redirection issue. This is authorization issue and I find solution here. With this guide uploadify and asp mvc work like a charm.

Comments

0

I also got error in Firefox.... Everythings seams work fine in IE.

So I started HttpScoop(Fiddler on PC) and viewed the result pane. I was a permanent redirect to my auhenticate view. The problem as I see it is that the swf/iframe needs to be authenticated. I check if there's a cookie sent in form and it contains (upload) in path. Sure this can be more finetuned, but it will do for now.

Solution I did was: I added a FormAuth key to uploadify. I then grab it in my AdminAttribute.

var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)";



$("#file_upload").uploadify({
            'formData'      : {'ListId' : '@modelData.ListId', 'folder' : '@path', 'sessionid' : '@Session.SessionID', 'token' : auth},
    ...

And the AllowAdminAttribute:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class AllowAdminAttribute : System.Web.Mvc.AuthorizeAttribute
{
    public AllowAdminAttribute(){
    }

    /// <summary>
    /// Authorize user
    /// </summary>
    /// <param name="filterContext">AuthorizationContext filterContext</param>
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        bool hasCookie = filterContext.HttpContext.Request.Params["token"] != null;

        if (filterContext.HttpContext.Request.Path.Contains("upload") && hasCookie){
        }
        else
        {
            if (!filterContext.HttpContext.User.IsInRole("admin"))
                filterContext.Result = GetFailedResult();
        }
    }

    /// <summary>
    /// Display the login view if user is a non admin and tries to access admin area.
    /// </summary>
    /// <returns>A login view</returns>
    private ActionResult GetFailedResult()
    {
        return new ViewResult { ViewName = "/Views/Shared/LogOn.cshtml" };
    }
}

I also needed to add it to my AdminAuthorize class

public sealed class AdminAuthorize : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        bool adminAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAdminAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAdminAttribute), true);

        //Hack for upload(Uploadify will break)
        bool hasCookie = filterContext.HttpContext.Request.Params["token"] != null;

        if (filterContext.HttpContext.Request.Path.Contains("upload") && hasCookie){
            adminAuthorization = false;
        }

        if (adminAuthorization)
        {
            base.OnAuthorization(filterContext);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.