Search code examples
c#asp.netajaxautopostback

Cannot get the uploaded file name while using ajax


I am using Ajax file upload control to upload file in which it works fine but when i a check box or radiobutton which has autopostback property set to true and I check then the FileName returns value null

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    try
    {
        if (AsyncFileUpload1.IsUploading)
        {
            AsyncFileUpload1.SaveAs(MapPath("~/Images/Accounts/" + AsyncFileUpload1.FileName));
        }
    }
    catch (Exception ex)
    {

    }
}

after uploading then any postback control fired it simply returns null and viewstate also not worked


Solution

  • use viewstate is a server control which will be clear by using postback so first store the value in a javascript and then use it as follows

    THE Java Script Method:

     function OnClientAsyncFileUploadComplete(sender, args) {
                          __doPostBack('filename', args.get_fileName());
    
            }
    
            function saveLogo(ImageName) {
                __doPostBack('filename', args.get_fileName());
            }
    

    in page load plese put the following code:

    IN .CS

               if (IsPostBack)
                {
    
                    string eventTarget = this.Request["__EVENTTARGET"];
                    if (eventTarget == "filename")
                    {
                        ViewState["EIName"] = this.Request["__EVENTARGUMENT"].ToString();
                    }
                }
    

    ViewState["EIName"] contains the file name

    you can use any where in the page