0

I have configuration problems with uploadify (v.2.1.4) and my MVC 3 project. Here's the code which returns the HTTP 302 code.

        @{string auth = @Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;}

        $("#fileuploader").uploadify({
            uploader: '@Url.Content("~/Scripts/uploadify.swf")',
            script: '@Url.Action("Upload", "Control")',
            scriptData: { token: "@auth" },
            fileDataName: 'file',
            buttonText: 'Upload file',
            multi: false,
            sizeLimit: 22222222222,
            simUploadLimit: 1,
            cancelImg: '@Url.Content("~/Images/uploadify-cancel.png")',
            auto: true,
            onError: function(event, queueID, fileObj, errorObj) {
                alert("Error!!! Type: [" + errorObj.type + "] Info [" + errorObj.info + "]");
            },
            onComplete: function (event, queueId, fileObj, response, data) {
                alert(response);
            }
        });

public class ControlController : Controller
{        
    [HttpPost]        
    public ActionResult Upload(string token, HttpPostedFileBase file)
    {
        if (file != null && file.ContentLength > 0)
        {
            var appData = Server.MapPath("~/app_data");
            var filename = Path.Combine(appData, Path.GetFileName(file.FileName));
            file.SaveAs(filename);
        }
        return Json(true);
    }
}

1) The controller's action is not being fired

2) I've found that topic Getting Uploadify to work with asp.net-mvc, but if I use that attribute to my controller, I see that "AuthenticationToken" is null (I'm logged in)

3) If I set the uploadify option 'method' to 'post' I get the #2032 error

EDIT

The controller is the Admininistration controller, so I use that Attribute to it:

    protected override bool AuthorizeCore(HttpContextBase httpContext) {

        if (!HttpContext.Current.User.Identity.IsAuthenticated)
            return false;

        if (admin && !um.IsAdmin(HttpContext.Current.User.Identity.Name))
            return false;

        return true;
    } 

which returns true. I've noticed, if I remove this attribute, the uploads started working. But I need that Attribute

2
  • @Andrew Barber: Hi Andrew! Did you have any luck with this? I'm running into the same exact issue. Commented Apr 15, 2012 at 1:42
  • @jaj it wasn't me; it was tony. But he was also sent a notification of your comment. Commented Apr 15, 2012 at 5:43

1 Answer 1

1
it's help you.
var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)";
    var ASPSESSID = "@(Session.SessionID)";

    $("#uploadifyLogo").uploadify({
        ...
        'scriptData': { 'ASPSESSID': ASPSESSID, 'AUTHID': auth  }
    });

In Global.asax :

protected void Application_BeginRequest(object sender, EventArgs e)
    {
      /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */
        try
        {
            string session_param_name = "ASPSESSID";
            string session_cookie_name = "ASP.NET_SessionId";

            if (HttpContext.Current.Request.Form[session_param_name] != null)
            {
                UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
            }
            else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
            {
                UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
            }
        }
        catch
        {
        }

        try
        {
            string auth_param_name = "AUTHID";
            string auth_cookie_name = FormsAuthentication.FormsCookieName;

            if (HttpContext.Current.Request.Form[auth_param_name] != null)
            {
                UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
            }
            else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
            {
                UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
            }

        }
        catch
        {
        }
    }

    private void UpdateCookie(string cookie_name, string cookie_value)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
        if (null == cookie)
        {
            cookie = new HttpCookie(cookie_name);
        }
        cookie.Value = cookie_value;
        HttpContext.Current.Request.Cookies.Set(cookie);
    }
Sign up to request clarification or add additional context in comments.

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.