Post a file to remote server using HTTPWEBREQUEST
-
Hello, Below is a piece of code, where in I am trying to post a file from my pc to remote development server. For some reason it does not seem to be working. I don't see the file on the remote server.
private void button2\_Click(object sender, EventArgs e) { FileStream filetoupload = new FileStream("C:\\\\projects\\\\testfile.csv", FileMode.Open); Uri address = new Uri("http://devsrvr/hints/"); HttpWebRequest webreq = HttpWebRequest.Create(address) as HttpWebRequest; // set type to post webreq.Method = "POST"; webreq.ContentType = "application/x-www-form-urlencoded"; webreq.Timeout = 15 \* 1000; // data we want to send, which is pretty much a file byte\[\] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString()); webreq.ContentLength = bytedata.Length; try { using (Stream poststream = webreq.GetRequestStream()) { poststream.Write(bytedata, 0, bytedata.Length); } MessageBox.Show("done!!"); } catch (Exception ex) { ex.InnerException.ToString(); ex.Source.ToString(); ex.Message.ToString(); } }
-
Hello, Below is a piece of code, where in I am trying to post a file from my pc to remote development server. For some reason it does not seem to be working. I don't see the file on the remote server.
private void button2\_Click(object sender, EventArgs e) { FileStream filetoupload = new FileStream("C:\\\\projects\\\\testfile.csv", FileMode.Open); Uri address = new Uri("http://devsrvr/hints/"); HttpWebRequest webreq = HttpWebRequest.Create(address) as HttpWebRequest; // set type to post webreq.Method = "POST"; webreq.ContentType = "application/x-www-form-urlencoded"; webreq.Timeout = 15 \* 1000; // data we want to send, which is pretty much a file byte\[\] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString()); webreq.ContentLength = bytedata.Length; try { using (Stream poststream = webreq.GetRequestStream()) { poststream.Write(bytedata, 0, bytedata.Length); } MessageBox.Show("done!!"); } catch (Exception ex) { ex.InnerException.ToString(); ex.Source.ToString(); ex.Message.ToString(); } }
-
-
vanikanc wrote:
There are no errors in the program.
There is, even though it doesn't throw an exception. Consider below line;
byte[] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString());
"filetoupload.ToString()" would result in "System.IO.FileStream". And that'd be passed to the "GetBytes"-function. Try something like below;
byte[] bytedata = File.ReadAllBytes("C:\\projects\\testfile.csv");
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
Hello, Below is a piece of code, where in I am trying to post a file from my pc to remote development server. For some reason it does not seem to be working. I don't see the file on the remote server.
private void button2\_Click(object sender, EventArgs e) { FileStream filetoupload = new FileStream("C:\\\\projects\\\\testfile.csv", FileMode.Open); Uri address = new Uri("http://devsrvr/hints/"); HttpWebRequest webreq = HttpWebRequest.Create(address) as HttpWebRequest; // set type to post webreq.Method = "POST"; webreq.ContentType = "application/x-www-form-urlencoded"; webreq.Timeout = 15 \* 1000; // data we want to send, which is pretty much a file byte\[\] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString()); webreq.ContentLength = bytedata.Length; try { using (Stream poststream = webreq.GetRequestStream()) { poststream.Write(bytedata, 0, bytedata.Length); } MessageBox.Show("done!!"); } catch (Exception ex) { ex.InnerException.ToString(); ex.Source.ToString(); ex.Message.ToString(); } }
I'm not surprised it's not working. As a small point, could you please post code that stands a chance of compiling. There isn't a HttpWebRequest.Create - it's WebRequest.Create. Your problem here is that you are just reading the data into a stream, which you then dispose of. What do you think the stream at the server end is going to do? You actually need something there to take that stream and write it out to a file. That's the part you're missing here.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
I'm not surprised it's not working. As a small point, could you please post code that stands a chance of compiling. There isn't a HttpWebRequest.Create - it's WebRequest.Create. Your problem here is that you are just reading the data into a stream, which you then dispose of. What do you think the stream at the server end is going to do? You actually need something there to take that stream and write it out to a file. That's the part you're missing here.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierActually, using HttpWebRequest.Create I was able to download a file from remote server to a specified location. Now, I want to be able to upload a file to a remote server using
HttpWebRequest.Create(
. The code works using WebClient class, to upload and download files, but there is not much error trapping that could be coded for. On research the better option was to use
HttpWebRequest
. Having an issue trying to post a file
-
vanikanc wrote:
There are no errors in the program.
There is, even though it doesn't throw an exception. Consider below line;
byte[] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString());
"filetoupload.ToString()" would result in "System.IO.FileStream". And that'd be passed to the "GetBytes"-function. Try something like below;
byte[] bytedata = File.ReadAllBytes("C:\\projects\\testfile.csv");
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]