2

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i did the code like this o.FileNames.ToList().ForEach(file => { string FileName = o.FileName; string filenames = Path.GetFileName(o.FileName); FileName = FileName.Replace(filenames, "").ToString(); FTPUtility obj = new FTPUtility(); obj.UploadDocsToFTP(FileName, filenames); });
it throws an error , Could not find file 'C:\Users\razim\Desktop\log_off.pnglog_off.png'. and this image is available in my desktop\
@RazimKhan It doesn't seem to be related to OpenFileDialog, Which line of code causes this error? Are sure your file exists there? Did you try to run your program as Administrator? (or your visual studio)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.