Code Rider
Code Rider

Reputation: 2063

Async File Upload in Asp.net

I know there are lots of examples and samples of code to upload the file asynchronously, but unfortunately no one is working for me.

I placed the update panel but File Uploader doesn't work in update panel. I have 4 text fields and a file uploader on the page. I want to save all information and want to upload file without any postback.

I saw AjaxFileUpload control of ajax control toolkit but it uploads the file instantly when we select it. But i want to upload the file on click of the button.

Please let me know any one has solution for it.

Thanks

Upvotes: 0

Views: 1860

Answers (2)

Regis
Regis

Reputation: 1

I was just having the same problem. All the examples I saw online did not work for me either. After stripping down the code, I figured I had to use ToolKitScriptManager and not the standard asp:ScriptManager.
I have not use the the toolkit before so I don't know if this is a new requirement with the latest toolkit since all the older examples I read used the ScriptManager and said it is supposed to work fine.

Here is the code behind to save from a button click:

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    AjaxControlToolkit.AsyncFileUpload afu = (AjaxControlToolkit.AsyncFileUpload)sender;

    if (afu.HasFile)
    {
        Session["AsyncFileUploadImage"] = afu;
    }

}
protected void AsyncFileUpload1_UploadedFileError(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    ...
}
protected void btnSave_Click(object sender, EventArgs e)
{       
    if (Session["AsyncFileUploadImage"] != null)
    {
        this.AsyncFileUpload1 = (AjaxControlToolkit.AsyncFileUpload)Session["AsyncFileUploadImage"];
        this.AsyncFileUpload1.SaveAs(Path.Combine(Request.PhysicalApplicationPath,"images", "uploads", this.AsyncFileUpload1.FileName)); //make sure to save in directory with write permissions
        this.lblStatus.Text = "You uploaded " + AsyncFileUpload1.FileName;
    }
    else
    {
      ...
    }


}

Upvotes: 0

mason
mason

Reputation: 32694

The Ajax Control Toolkit you mentioned as an AjaxFileUpload that you can use. And yes, it can be hooked up to a button as demonstrated in the page I linked to. Please use it and then let us know specifically what problems you run into with it.

Upvotes: 1

Related Questions