C# code for uploading multiple files on a server

Do you require to upload multilpe files to the server from your application? Are you looking for C# code for uploading multiple files to the server together in one go? Check out a C# program for uploading multiple files to a server in a single click, here in this article.

Are you looking for C# program which will allow users to upload multiple files to the server using the fileupload control? Generally we upload one file at a time to the server, but here, an application is allowed to select serveral files from the folder and upload it on the server. After selecting one file, if user wants to add another file then, there is a button called "Add More" which will give an option to the user to select other file without creating any other extra controls or recreation of controls.

Finally when the selection of files is completed then the user will click the "Submit" button which will do submission of the files to the server.

Here is the sample code for a Multiple File Uploader Application. You can modify it for further extension and change according to your requirements.

Flow of control for Muliple File Uploading to Server Program

1)Click on the "Select" button in the file upload control. It will show a dialog box. There you can select file and click OK.
2)If you want to select or add other file with the current one then click on the add more button, and then select other file by repeating step 1.
3)Now you can add any other and as many files you like.
4)Finally click on the Submit button.
5)See the folder in the application which is used to save the files. Here I have created a folder in the application with the name Save to save the uploaded files there.

Note:You can change/modify this code as per your requirement.

Multiple file uploading code, for adding new file:

protected void addmore_Click(object sender, EventArgs e)

if (FileUpload1.HasFile)
string filename = FileUpload1.PostedFile.FileName.ToString();
HiddenField1.Value += filename + ",";


Multiple file uploading code for uploading all the files in a single click:

protected void submit_Click(object sender, EventArgs e)
if (!FileUpload1.HasFile)
FolderBrowserDialog obj = new FolderBrowserDialog();

if (FileUpload1.HasFile)
HiddenField1.Value += FileUpload1.PostedFile.FileName.ToString() + ",";
string files = HiddenField1.Value;
int i = 0;
int count = files.IndexOf(",", i);
for (int x = 0; x < count; x++)
string filepath = files.Substring(i, count - i).Trim(',');
i = count;
count = files.IndexOf(',', count + 1);
int endindex = filepath.LastIndexOf("\\");
string filenewname = Server.MapPath("~/save") + filepath.Substring(endindex, filepath.Length - endindex);
//Open a FileStream to the source file
FileStream fin = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);

//Open a FileStream to the destination file
FileStream fout = new FileStream(filenewname, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

//Create a byte array to act as a buffer
Byte[] buffer = new Byte[32];
Console.WriteLine("File Copy Started");

//Loop until end of file is not reached

while (fin.Position != fin.Length)
//Read from the source file
//The Read method returns the number of bytes read
int n = fin.Read(buffer, 0, buffer.Length);

//Write the contents of the buffer to the destination file
fout.Write(buffer, 0, n);

//Flush the contents of the buffer to the file

//Close the streams and free the resources
Console.WriteLine("File Copy Ended");
catch (Exception ex)


Below is the ASPX code for the multiple file uploading program page:

<asp:Button ID="Button1" runat="server" Text="Button" />
< br />
<div id="upload">
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="True" />
<asp:Button ID="submit" runat="server" Text="submit" OnClick="submit_Click" />
<asp:Button ID="clear" runat="server" Text="clear" />
<asp:Button ID="addmore" runat="server" Text="addmore" OnClick="addmore_Click" />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server" Height="89px" TextMode="MultiLine" Width="205px"></asp:TextBox>

You can find the file for the program to upload multiple files to server attached below:


  • uploading mutliple files inasp.net using fileupload (44138-43243-uploading-mutliple-files-inasp.net-using-fileupload.rar)
  • Comments

    No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: