Asp.net AJAX文件上传控件无法正常工作与URL重写 [英] Asp.net ajax file upload control not working with url rewriting

查看:91
本文介绍了Asp.net AJAX文件上传控件无法正常工作与URL重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从asp.net AJAX控件工具包中的文件上传控件。而我的web应用程序使用URL重写。

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

我的AJAX文件上传控件标记了跟随,

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

和我的事件是在服务器上($ C $后面三)就像下面,

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


    }

和我的使用Ajax文件上传相关的客户端事件之后,

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

什么似乎一个问题是,在 AJAX扩展的文件上传控件无法正常工作与URL重写。

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

我的code相关的URL在Global.asax中重写是继

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

            }
        }


    }

我怎样才能使它发挥作用。如何使用AJAX文件上传控制,URL重写?

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

任何帮助将AP preciated。

Any help will be appreciated.

感谢

推荐答案

所有我需要做的就是修改

All I needed to do was to make changes to

HttpContext.Current.RewritePath(string)

弦不得不进行调整

string had to be tweaked

这篇关于Asp.net AJAX文件上传控件无法正常工作与URL重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆