0

I have use Ajax file upload control in asp.net and it is working fine .Problem is that when all image being loaded complite then i want to reload same page . How can I do it? My code is as following .

<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# code

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 Answers 2

0

Add this to your AjaxUploadControl:

OnClientUploadComplete="ClientUploadComplete"

Then this part stays as is on server side:

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

Last part is to add this to your client side scripts:

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

Add a client side uploadcomplete event and refresh your page from there

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

  />

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.