views:

397

answers:

2

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?

A: 

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;
  ...
}
Mitch Wheat
A: 

using (WebClient wc = new WebClient()) { wc.Proxy = p; ... }

I had been trying the same thing, have a similar code and i tried your solution but i have a doubt, what do i write in between??

Imcl