Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,144 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,079 people online right now. Registration is fast and FREE... Join Now!




Image upload issues {RESOLVED}

 
Reply to this topicStart new topic

Image upload issues {RESOLVED}

PsychoCoder
post 8 Oct, 2008 - 05:03 PM
Post #1


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,919



Thanked 117 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


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

csharp

private void cmdUpload_Click(object sender, EventArgs e)
{
DirectoryInfo directory = new DirectoryInfo(txtDirectory.Text);
string uploadURL = "mypagetohandleuploads";
string response = string.Empty;
try
{
//Create a WebClient object
WebClient client = new WebClient();

//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

csharp

protected void Page_Load(object sender, EventArgs e)
{
string tempFileName = null;
HttpFileCollection files = Request.Files;
try
{
for (int i = 0; i < files.Count; i++)
{
tempFileName = Server.MapPath("~/images/tagz/completed/") + files[i].FileName.Substring(files[i].FileName.LastIndexOf("\\") + 1); ;//@"myuploadpath" + files[i].FileName.Substring(files[i].FileName.LastIndexOf("\\") + 1);
files[0].SaveAs(tempFileName);
}
}
catch (Exception ex)
{
SqlManager sql = new SqlManager();
string ip = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
string message = ex.Message.ToString();
int eventType = 3;
string username = "angelzdesigns.com_fp";
sql.LogSystemEvent(ref eventType, ref message, ref ip, ref username);
}
}

Anyone got any better ideas for upload files from a desktop application?
User is online!Profile CardPM

Go to the top of the page

Jayman
post 8 Oct, 2008 - 06:28 PM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Is the an FTP or HTTP upload?

Are you able to upload a file by other means to verify that you have the necessary permissions?
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 8 Oct, 2008 - 06:39 PM
Post #3


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,919



Thanked 117 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


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
User is online!Profile CardPM

Go to the top of the page

zakary
post 9 Oct, 2008 - 04:41 AM
Post #4


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 401



Thanked 6 times

Dream Kudos: 175
My Contributions


PsychoCoder, I know only a little about Web base apps but have you tried using WebRequest. I have seen something like this before.
csharp

WebRequest wr = WebRequest.Create(@"myurl");
//should be your NetworkCredential
wr.Credentials = credentials;


This post has been edited by zakary: 9 Oct, 2008 - 04:42 AM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 9 Oct, 2008 - 05:50 AM
Post #5


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


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 icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 3 Nov, 2008 - 12:44 PM
Post #6


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,919



Thanked 117 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Well I'm revisiting this uploader (that is needed to complete this project. I've changed my code to try this

csharp

private void cmdUpload_Click(object sender, EventArgs e)
{
DirectoryInfo directory = new DirectoryInfo(txtDirectory.Text);
string uploadURL = "** Removed For Security **";
string response = string.Empty;
try
{
//Create a WebClient object
WebClient client = new WebClient();

//create network credentials
NetworkCredential credentials = new NetworkCredential("** Removed **", "** Removed **");

//create a credentials cache
CredentialCache cache = new CredentialCache();

//add our credentials to our cache
cache.Add(new Uri(uploadURL), "Basic", credentials);

//set the credentials for our WebClient
client.Credentials = cache;

client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("CharSet", "UTF-8");

//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");
}
}


Then on my upload page I have this

csharp

protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection files = Request.Files;
try
{
for (int i = 0; i < files.Count; i++)
{
string file = files[i].FileName.Substring(files[i].FileName.LastIndexOf("\\") + 1);
files[0].SaveAs(Server.MapPath("~/images/tagz/completed/") + file);
}
}
catch (Exception ex)
{
SqlManager sql = new SqlManager();
string ip = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
string message = ex.Message.ToString();
int eventType = 3;
string username = "** Removed **";
sql.LogSystemEvent(ref eventType, ref message, ref ip, ref username);
}
}


I'm no longer getting the error logged to my database, but my files aren't uploaded either crazy.gif
User is online!Profile CardPM

Go to the top of the page

PsychoCoder
post 3 Nov, 2008 - 04:17 PM
Post #7


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,919



Thanked 117 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


This is really starting to drive me batty. now I'm trying to do this with an HttpWebRequest, Stream and FileStream. here is the code I'm using:

csharp

public void UploadFile(ref string file, ref string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.Method = "GET";
request.ContentType = "multipart/form-data";
request.AllowWriteStreamBuffering = true;

//create network credentials
NetworkCredential credentials = new NetworkCredential("** REMOVED **", "** REMOVED **");

//create a credentials cache
CredentialCache cache = new CredentialCache();

//add our credentials to our cache
cache.Add(new Uri(url), "Basic", credentials);

//set the credentials for our WebClient
request.Credentials = credentials;

//Retrieve request stream
Stream requestStream = request.GetRequestStream();

// 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
Attached Image

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? blink.gif

EDIT: When I try to use POST instead of GET I get (at request.GetResponse();)
Attached Image

When I try to use PUT I get
Attached Image
User is online!Profile CardPM

Go to the top of the page

PsychoCoder
post 3 Nov, 2008 - 11:04 PM
Post #8


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,919



Thanked 117 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


I resolved the issue using a Web Service. I'll post the code tomorrow (and possibly write a tutorial on it too)
User is online!Profile CardPM

Go to the top of the page

AdamSpeight2008
post 3 Nov, 2008 - 11:15 PM
Post #9


LINQ D.I.C.

Group Icon
Joined: 29 May, 2008
Posts: 749



Thanked 48 times

Dream Kudos: 2075
My Contributions


ohno.gif PsychoCoder's powers wizard.gif are weakening, need to find more mortals pimp.gif to worship at his alter of greatness. To restore the Ying-Yang yinyang.gif
User is online!Profile CardPM

Go to the top of the page

PsychoCoder
post 4 Nov, 2008 - 08:46 AM
Post #10


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,919



Thanked 117 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Huh?! Hey I don't know everything, just like 95% of it tongue.gif
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 12:46PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month