简体   繁体   中英

AjaxFileUpload Iterate uploaded files afer files are uploaded

I am using AjaxFileUpload control in Ajaxcontrol Toolkit.

Sample code is as below.

<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" ThrobberID="myThrobber" 
AllowedFileTypes="doc,jpg,jpeg,mp3" MaximumNumberOfFiles="10" runat="server"
OnUploadComplete="AjaxFileUpload1_UploadComplete" 
Width="450px" />

<asp:Button ID="btnCheckFiles" Text="CheckFiles" runat="server"/>

I can save the files in AjaxFileUpload1_UploadComplete event

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
   AjaxFileUpload1.SaveAs(filepath);
}

Above is working fine but instead of saving files in UploadComplete event, I want to save them in Button Click event of another button after files are uploaded, here say in click event of btnCheckFiles, something like below

protected void btnCheckFiles_Click(object sender, EventArgs e)
{
  // Iterate all files here uploaded 
  // for each file in ajaxfileupload control
  //iterate and save each file to a path
}

Is there some way to achieve the above requirement?

Make changes in your aspx page as follow:

<asp:HiddenField id="hdnFileIDs" runat="server" Value="" />

<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" ThrobberID="myThrobber" 
AllowedFileTypes="doc,jpg,jpeg,mp3" MaximumNumberOfFiles="10" runat="server"
OnUploadComplete="AjaxFileUpload1_UploadComplete" 
OnClientUploadComplete="onClientUploadComplete"
Width="450px" />

Add javascript block as follow:

 <script type="text/javascript">    
function onClientUploadComplete(sender, e) {
                var id = e.get_fileId();
                var objHdnFileIDs = document.getElementById('<%=hdnFileIDs.ClientID%>');
                objHdnFileIDs.value = objHdnFileIDs.value + id + ',';
               }
</script>

In your .cs page

  protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs file)
    {
         Session["fileContents_" + file.FileId] = file.GetContents();
         Session["fileExtension_" + file.FileId] = file.FileName.Substring(file.FileName.LastIndexOf('.'));
       //AjaxFileUpload1.SaveAs(filepath);
    }

protected void btnCheckFiles_Click(object sender, EventArgs e)
{
  // Iterate all files here uploaded 
   if (!string.IsNullOrEmpty(hdnFileIDs.Value))
        {
            string strFileIDs = hdnFileIDs.Value;
            string[] arrFileIDs = strFileIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (arrFileIDs != null && arrFileIDs.Length > 0)
            {
                foreach (string strFileID in arrFileIDs)
                {
                    var fileContents = (byte[])Session["fileContents_" + strFileID];
                    var fileExtension = (string)Session["fileExtension_" + strFileID];

                    File.WriteAllBytes(filepath + strFileID + fileExtension, fileContents);
                    Session.Remove("fileContents_" + strFileID);
                    Session.Remove("fileExtension_" + strFileID);
                }
            }
        }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM