Join 131,546 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,259 people online right now. Registration is fast and FREE... Join Now!
I'm working on an image upload application for a client, and am trying to use WebClient.UploadFile passing in my login credentials (which should technically work) but I keep getting an Access Denied error to the file I'm trying to save.
According to all the documentation I've read on this the following code should work
//create network credentials NetworkCredential credentials = new NetworkCredential("myusername", "mypassword", "mydomain");
//create a credentials cache CredentialCache cache = new CredentialCache(); //add our credentials to our cache cache.Add(new Uri(@"myurl"), "Basic", credentials);
//set the credentials for our WebClient client.Credentials = cache;
//loop through each of the files in our directory foreach (FileInfo file in directory.GetFiles()) { //Create a byte array and set it equal to the response returned by the //WebClient upload file method. byte[] res = client.UploadFile(uploadURL, "POST", directory + "\\" + file.Name);
// Decode and store the response. response += (Encoding.ASCII.GetString(res)); rtbStatus.Text += response; } } catch (Exception ex) { MessageBox.Show("Error trying to upload file: " + ex.Message, "Upload Error"); } }
Here's the code I have on my page to handle the upload
It's an HTTP upload, I can upload from an ASPX page on the server, but from the application it isn't working. I was thinking of using a FileStream/BinaryWriter combination but from what I've read the WebClient is supposed to be one of the best ways to go. I'm confused lol
Since it's saying access denied, why not try it with a stream? Just to see if you get the same problem~
It's kinda different, but when I was working on my Twitter client's post method, I tried a client with UploadString and I kept getting similar errors. I used a stream and it worked fine
//loop through each of the files in our directory foreach (FileInfo file in directory.GetFiles("*.gif")) { byte[] array = Encoding.ASCII.GetBytes(directory + "\\" + file.Name);
//Create a byte array and set it equal to the response returned by the //WebClient upload data method. byte[] res = client.UploadData(uploadURL, array);
// Decode and store the response. response = (Encoding.ASCII.GetString(res)); rtbStatus.Text += response; } } catch (Exception ex) { MessageBox.Show("Error trying to upload file: " + ex.Message, "Upload Error"); } }
// Open the local file FileStream stream = new FileStream(file, FileMode.Create);
// Allocate byte buffer to hold file contents byte[] data = new byte[4096];
// loop through the local file reading each data block // and writing to the request stream buffer int size = stream.Read(data, 0, data.Length); while (size > 0) { request.ContentLength = data.Length; requestStream.Write(data, 0, size); size = stream.Read(data, 0, data.Length); }
stream.Close(); requestStream.Close();
request.GetResponse(); }
And this gives me this error
I've done a lot of Googling on this and am not finding anything helpful. Anyone got anymore ideas that I haven't thought of?
EDIT: When I try to use POST instead of GET I get (at request.GetResponse();)