简体   繁体   中英

Uploading files to server

I am trying to upload a file from my Windows application to the server into a particular Folder using C#. However, I am getting an exception:

"An exception occurred during a WebClient request".

Here is my code:

for (int i = 0; i < dtResponseAttach.Rows.Count; i++)
{
  string filePath = dtResponseAttach.Rows[i]["Response"];

  WebClient client = new WebClient();
  NetworkCredential nc = new NetworkCredential();

  Uri addy = new Uri("http://192.168.1.4/people/Attachments/");
  client.Credentials = nc;
  byte[] arrReturn = client.UploadFile(addy, filePath);
  Console.WriteLine(arrReturn.ToString());
}

What could be the reason for this exception?

If you are not filling in the NetworkCredential , then I'm pretty sure you should not attach one.

Another possibility, is that you are going through a proxy, and would need to add the proxy details:

WebProxy p = new WebProxy ("192.168.10.01", true);
p.Credentials = new NetworkCredential ("username", "password", "domain");
using (WebClient wc = new WebClient())
{
  wc.Proxy = p;
  ...
}

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