页面重定向不工作Ajax文件上传控件

时间:2014-02-25 10:58:40

标签: asp.net

我在asp.net中使用Ajax文件上传控件,它运行正常。问题是,当所有图像被加载完全然后我想重新加载相同的页面。  我该怎么做?我的代码如下。

<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" AllowedFileTypes="jpg,jpeg,png,gif" 
     MaximumNumberOfFiles="15" OnUploadComplete="File_Upload" Width="500px"   OnClientUploadComplete="OnClientUploadComplete"
     OnClientUploadError="showUploadError" />

<script type="text/javascript">
     function OnClientUploadComplete() {
         $get("<%=lblMessage.ClientID%>").innerHTML = "File Uploaded Successfully Please refresh the page";

        // window.location = "ModelImageUpload.aspx";
         //$(location).attr('href', "ModelImageUpload.aspx");
          window.location = window.location.href;

     }
     function showUploadError(sender, args) {
         alert(args.get_errorMessage());
     }

    </script>

C#代码

protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
{
    lblMessage.Text = string.Empty;
    string sqlMsg = string.Empty;

    int count = 0;
    clsModelImage objModImg = new clsModelImage();
    clsDataAccessLayer objDAL = new clsDataAccessLayer();
    string filename = e.FileName;
    string ImgName = DateTime.Now.Ticks.ToString().Trim() + "_" + filename;
    FileInfo oldfile = new FileInfo((ImgFolderPath) + ImgName);
    if (oldfile.Exists)
    {
        oldfile.Delete();
    }

    objModImg.modelId = Convert.ToInt32(ModelId);
    objModImg.imageName = ImgName;
    sqlMsg = objDAL.ModelImageUpload(objModImg);
    if (sqlMsg == "1")
    {
        count++;
        string ImgPath = Server.MapPath(ImgFolderPath);
        AjaxFileUpload1.SaveAs(@ImgPath + ImgName);
        lblMessage.Text = sqlMsg;
        lblMessage.ForeColor = Color.Red;
    }
    else
    {
       //-----

        //Response.Redirect("ModelImageUpload.aspx");
       // System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "OnClientUploadComplete();", true);

        AjaxFileUpload1.Attributes.Add("OnClientUploadComplete", "Return  OnClientUploadComplete");
        AjaxFileUpload1.BackColor = Color.Red;


    }         

} 

2 个答案:

答案 0 :(得分:0)

将此添加到您的AjaxUploadControl:

OnClientUploadComplete="ClientUploadComplete"

然后这部分保持在服务器端:

AjaxFileUpload1.Attributes.Add("OnClientUploadComplete", "Return  OnClientUploadComplete");

最后一部分是将此添加到客户端脚本:

        function ClientUploadComplete() {
        var f = $(".filename").html();
        window.location.replace("upload.aspx?filename=" + f);
    }

答案 1 :(得分:0)

添加客户端上载完成事件并从此处刷新页面

<cc1:AsyncFileUpload OnClientUploadComplete="clientuploadComplete" runat="server"
    ID="AsyncFileUpload1" OnUploadedComplete="FileUploadComplete"

  />

<script type = "text/javascript">
function clientuploadComplete(sender) {
    window.location = window.location.href;
}
</script>

发件人:https://forums.asp.net/t/1656787.aspx?Async+File+Upload+issue+with+redirect