0

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);
  }
4

1 回答 1

2

通过属性添加用于上传开始的客户端处理程序,OnClientUploadStarted并按如下方式使用它:

<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);
}

使用此代码,您需要自己做的就是提供正确的 id 值;

于 2013-05-27T15:52:21.457 回答