Asp.net ajax文件上传控件不能与url重写

时间:2014-08-21 09:42:45

标签: c# asp.net ajax asp.net-ajax ajaxcontroltoolkit

我有一个来自asp.net Ajax控件工具包的文件上传控件。我的Web应用程序正在使用URL重写。

我的ajax文件上传控件标记如下,

 <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" />

我在服务器上的事件(代码隐藏)如下,

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) { }


    }

我的客户端事件与ajax文件上传器关联,

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;
    }

似乎有问题的是 ajax扩展文件上传控件无法使用url重写。

我的代码与global.asax中的url重写有关,

  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);

            }
        }


    }

我怎样才能让它发挥作用。如何使用url重写的ajax文件上传控件?

任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

我需要做的就是对

进行更改
HttpContext.Current.RewritePath(string)
必须调整

字符串