0

What's the most efficient way to upload files to a server through http post?

I'm using WebClient right now and threads:

public static void Uploadfile(string file_path, string SERVER_URL)
    {
        string filename = file_path;//Asign a value anyway in case of error.
        try
        {
                filename = Path.GetFileName(file_path);

                //Parameter for pass the name of the file
                NameValueCollection parameters= new NameValueCollection();
                parameters.Add("file", filename);
                WebClient client = new WebClient();
                //PUT Request
                client.QueryString = parameters;
                byte[] rawResponse = client.UploadFile(SERVER_URL, "POST", file_path);
                Console.WriteLine(filename + " Uploaded!");
                client.Dispose();
        }
        catch (Exception err)
        {
            Console.WriteLine(filename + " NOT uploaded: " + err.Message);
        }
    }

I call the function in thread like that:

for(xxx)
    tasks.Add(Task.Run(() => Uploadfile(file_path, SERVER_URL)));

I open a instance for every file, is that bad? WebClient is the most efficient? Do you have any suggestion?

GMX
  • 950
  • 1
  • 14
  • 29

1 Answers1

0

If you cannot group your files, then you will need to upload them one-by-one. If you can group them, then it is advisable to create a file which will contain the desired paths of the files if it is not obvious, zip the files, upload the zip file, unpack it on the server and move the files to their desired location. You will need to pay attention to security though.

WebClient implements FTP uploading, see: Upload file to FTP using C#

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175