【问题标题】:Asp.net ajax file upload control not working with url rewritingAsp.net ajax 文件上传控件不适用于 url 重写
【发布时间】:2014-08-21 09:42:44
【问题描述】:

我有一个来自asp.net Ajax 控件工具包的文件上传控件。我的网络应用程序正在使用 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文件上传控件?

任何帮助将不胜感激。

谢谢

【问题讨论】:

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


    【解决方案1】:

    我需要做的就是改变

    HttpContext.Current.RewritePath(string)
    

    字符串需要调整

    【讨论】:

    • 能否请您粘贴代码如何以及在何处输入 httpcontext.current.rewritepath(string),我遇到了 ajax 和 context.rewritepath 的问题。我在 GLOBAL.asax 文件中手动重写重定向代码。
    • Global.asax 这正是我编写代码的地方。 Application_BeginRequest 事件内部
    • 能否请您粘贴完整的代码,它将帮助我了解我做错了什么......感谢
    • 我已经粘贴了与 url 重写相关的代码。更新的答案。请不要忘记支持我的回答。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多