Reputation: 347
I want to upload multiple files using single upload button. When I select more than one files it uploads and leaves rest of the files. Here is my code:
private void buttonBrowse_Click(object sender, EventArgs e)
{
try
{
var o = new OpenFileDialog();
o.Multiselect = true;
if (o.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string FileName = o.FileName;
string filenames = Path.GetFileName(o.FileName);
FileName = FileName.Replace(filenames,"").ToString();
FTPUtility obj = new FTPUtility();
if (obj.UploadDocsToFTP(FileName, filenames))
{
MessageBox.Show("File Uploaded Successfully...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
loadfiles();
}
}
else
{
MessageBox.Show("File Not Uploaded", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.LoadFiles();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Upvotes: 2
Views: 9769
Reputation: 125322
Set MultiSelect
property of OpenFileDialog
to true
and then use FileNames
property to get all selected files.
o.FileNames.ToList().ForEach(file =>
{
//upoaad each file separately
});
FileNames
Gets the file names of all selected files in the dialog box.
For example your code may be like this:
var o = new OpenFileDialog();
o.Multiselect = true;
if (o.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var ftp = new FTPUtility();
var failedToUploads= new List<string>();
var uploads= new List<string>();
o.FileNames.ToList().ForEach(file =>
{
var upload= ftp.UploadDocsToFTP(file, Path.GetFileName(file)));
if(upload)
uploads.Add(file);
else
failedToUploads.Add(file);
});
var message= string.Format("Files Uploaded: \n {0}", string.Join("\n", uploads.ToArray()));
if(failedToUploads.Count>0)
message += string.Format("\nFailed to Upload: \n {0}", string.Join("\n", failedToUploads.ToArray()));
MessageBox.Show(message);
this.LoadFiles();
}
This way you can show a message that informs the user about his uploaded files or which one of them that is failed to upload.
Upvotes: 2