Search code examples
c#asp.netajaxasp.net-ajaxajaxcontroltoolkit

Asp.net ajax file upload control not working with url rewriting


I have a file upload control from asp.net Ajax control toolkit. And my web application is using url rewriting.

My ajax file upload control mark up is following,

 <cc1:AsyncFileUpload Width="200px" ID="fu" runat="server" OnClientUploadError="uploadError"
                        OnClientUploadStarted="StartUpload" OnClientUploadComplete="UploadComplete" CompleteBackColor="Lime"
                        UploaderStyle="Modern" ErrorBackColor="Red" ThrobberID="Throbber" OnUploadedComplete="AsyncFileUpload1_UploadedComplete"
                        UploadingBackColor="#66CCFF" />

And my event that is on server(code behind) is like following,

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        string extension = Path.GetExtension(fu.PostedFile.FileName).ToLower();
        string fileType = null;

        switch (extension)
        {
            case ".gif":
                fileType = "image/gif";
                break;
            case ".jpg":
            case ".jpeg":
            case ".jpe":
                fileType = "image/jpeg";
                break;
            case ".png":
                fileType = "image/png";
                break;
            default:
                lblStatus.Text = "<br />Error - invalid file type.<br />";

                return;
        }

        System.Threading.Thread.Sleep(5000);
        //string q = Decrypt(Request.QueryString["pname"].ToString());
        string q = Request.QueryString["un"].ToString();
        string sql = "update AppUser set Pic=@pic where ProfileName='" + q + "'";
        SqlConnection con = new SqlConnection(constr);
        byte[] imageBytes = new byte[fu.PostedFile.InputStream.Length + 1];
        fu.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.AddWithValue("@pic", imageBytes);
        con.Open();
        int ret = cmd.ExecuteNonQuery();
        if (ret > 0) { }


    }

And my client side events associated with ajax file uploader are following,

function uploadError(sender, args) {
        document.getElementById('lblStatus').innerText = args.get_fileName(), "<span style='color:red;'>" + args.get_errorMessage() + "</span>";
    }

    function StartUpload(sender, args) {alert("en");

        document.getElementById('lblStatus').innerText = 'Uploading Started.';
    }

    function UploadComplete(sender, args) {
        var filename = args.get_fileName();
        var contentType = args.get_contentType();
        var text = "Size of " + filename + " is " + args.get_length() + " bytes";
        if (contentType.length > 0) {
            text += " and content type is '" + contentType + "'.";
        }
        document.getElementById('lblStatus').innerText = text;
    }

what seem to a problem is that the ajax extension file upload control not working with url rewriting.

My code related to url rewriting in global.asax is following,

  protected void Application_BeginRequest(object sender, EventArgs e)
    {

        string CurrentUrl = HttpContext.Current.Request.Url.AbsoluteUri;
        string[] s = CurrentUrl.Split('/');
        string ActionName = CurrentUrl.Split('/')[s.Length - 1];
        bool f = checkUserExist(ActionName);
        if (f == true)
        {
            CurrentUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower();
            s = CurrentUrl.Split('/');
            ActionName = CurrentUrl.Split('/')[s.Length - 1];

        }
        string originalPath = HttpContext.Current.Request.Path.ToLower();

        if (ActionName.Contains("AsyncFileUploadID"))
        {
            //string str = ActionName.Replace("alikhan/",@"/Auth/profile.aspx");//http://localhost:59287/auth/profile.aspx
            //HttpContext.Current.RewritePath(originalPath.Replace(str, ActionName));
            //HttpContext.Current.RewritePath(originalPath.Replace("Auth/profile.aspx", "Auth/profile.aspx"));
            HttpContext.Current.RewritePath("/auth/profile.aspx");

        }
        else
        {
            //alikhan?AsyncFileUploadID=fu1&rnd=0728572010062635
            if (!(ActionName.Contains("aspx") || ActionName.Contains("net") || ActionName.Contains(".") || ActionName.Contains("ashx")))
            {


                if (originalPath.Contains("/" + ActionName))
                {
                    HttpContext.Current.RewritePath(originalPath.Replace("/" + ActionName, @"/Auth/profile.aspx?un=" + ActionName));
                    // HttpContext.Current.Response.Redirect(@"/Auth/profile.aspx?un=" + ActionName);

                }
            }
            else
            {

                if (!(ActionName.Contains("ashx") || ActionName.Contains("aspx")))
                {

                    // if (!ActionName.Contains("AsyncFileUploadID"))
                    {
                        try
                        {
                            HttpContext.Current.RewritePath(originalPath.Replace(ActionName, ActionName));
                        }
                        catch (Exception ex)
                        {// HttpContext.Current.RewritePath(originalPath.Replace("fp.aspx", "fp.aspx"));
                        }
                    }

                }
                //HttpContext.Current.Response.Redirect("fp.aspx",true);

            }
        }


    }

How can I make it work. How can I use ajax file upload control with url rewriting?

Any help will be appreciated.

Thanks


Solution

  • All I needed to do was to make changes to

    HttpContext.Current.RewritePath(string)
    

    string had to be tweaked