Search code examples
asp.netajaxajaxcontroltoolkit

Pass parameters to server side with asyncfileupload (ajaxcontroltoolkit)


I use ASP.NET and i need an easy way to upload a file asynchronously. So I tried to use asyncfileupload (Ajax control toolkit) but I also need to pass parameters to the server side. How can I do that ? thanks.

Here is my code :

on client side :

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
 <asp:AsyncFileUpload ID="afuMedia" runat="server" UploaderStyle="Modern" OnUploadedComplete="afuMedia_UploadedComplete" />

on server side :

  protected void afuMedia_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
  {
      //int id = int.Parse(Request.QueryString["id"]);
      string mediaPath = ConfigurationParameters.MediaPath;
      string filePath = CurrentBrand.BrandCode + "\\" + CurrentCulture.CultureCode + "\\" + "highlights-" + id;
      string physicalPath = Path.Combine(MapPath("~/" + mediaPath), filePath);

      afuMedia.SaveAs(physicalPath);
  }

Solution

  • Add client handler for upload start via the OnClientUploadStarted property and use it as below:

    <asp:AsyncFileUpload ID="afuMedia" runat="server" UploaderStyle="Modern"
        OnUploadedComplete="afuMedia_UploadedComplete" 
        OnClientUploadStarted="afuMedia_OnClientUploadStarted" />
    
    function afuMedia_OnClientUploadStarted(sender, args){
        var id = 123;
        var url = sender.get_postBackUrl();
        url += url.indexOf("?") === -1 ? "?" : "&";
        url += ("id=" + id.toString());
        sender.set_postBackUrl(url);
    }
    

    With this code all that you need to do on you own it's to provide correct id value;