Uploadng Files to a Web Server

  • Thread starter Thread starter Derek Lakin
  • Start date Start date
D

Derek Lakin

I want to be able to upload some files from a PC to a web server from a C#
client application.
Having trawled the net and newsgroups I have put together the following
code. However, what should this.serverUrl be? Does this need to be an
ASP/PHP page capable of recieving a POSTed file or is it a destination
address on the server? In the case of the former, can someone please give me
an example of the relevant PHP file?

Regards,

Derek.

WebClient webClient = new WebClient();
try
{
FileStream fileStream = new FileStream(this.localPath, FileMode.Open);
Stream postStream = webClient.OpenWrite(this.serverUrl, "PUT");
byte [] buffer = new byte[4096];
byte [] tmp_buffer = null;
int nb = 0;
while(true)
{
nb = stream.Read(buffer, 0, 4096);
if(nb < 1)
break;
else if(nb != 4096)
{
tmp_buffer = new Byte[nb];
System.Array.Copy(buffer, tmp_buffer, nb);
postStream.Write(tmp_buffer, 0, tmp_buffer.Length);
}
else
postStream.Write(buffer,0,buffer.Length);
}
postStream.Close();
stream.Close();
}
catch (System.Exception exc)
{
MessageBox.Show(exc.ToString(), "Upload Failed");
}
 
Derek Lakin said:
I want to be able to upload some files from a PC to a web server from a C#
client application.
Having trawled the net and newsgroups I have put together the following
code. However, what should this.serverUrl be? Does this need to be an
ASP/PHP page capable of recieving a POSTed file or is it a destination
address on the server? In the case of the former, can someone please give me
an example of the relevant PHP file?

<snip>

It's just the URL to post to - whether that's ASP, CGI, PHP, Java
Servlet, whatever...
nb = stream.Read(buffer, 0, 4096);
if(nb < 1)
break;
else if(nb != 4096)
{
tmp_buffer = new Byte[nb];
System.Array.Copy(buffer, tmp_buffer, nb);
postStream.Write(tmp_buffer, 0, tmp_buffer.Length);
}
else
postStream.Write(buffer,0,buffer.Length);


Apart from anything else, why are you doing this? Why not just use:

postStream.Write(buffer,0,nb);
 
Thanks for the reply Jon. It is as I suspected.

Can anyone point me in the right direction for some PHP code to direct this
to, please?

Derek.

Jon Skeet said:
Derek Lakin said:
I want to be able to upload some files from a PC to a web server from a C#
client application.
Having trawled the net and newsgroups I have put together the following
code. However, what should this.serverUrl be? Does this need to be an
ASP/PHP page capable of recieving a POSTed file or is it a destination
address on the server? In the case of the former, can someone please give me
an example of the relevant PHP file?

<snip>

It's just the URL to post to - whether that's ASP, CGI, PHP, Java
Servlet, whatever...
nb = stream.Read(buffer, 0, 4096);
if(nb < 1)
break;
else if(nb != 4096)
{
tmp_buffer = new Byte[nb];
System.Array.Copy(buffer, tmp_buffer, nb);
postStream.Write(tmp_buffer, 0, tmp_buffer.Length);
}
else
postStream.Write(buffer,0,buffer.Length);


Apart from anything else, why are you doing this? Why not just use:

postStream.Write(buffer,0,nb);
 
"Derek Lakin" spoke:
I want to be able to upload some files from a PC to a web server
from a C# client application.
Having trawled the net and newsgroups I have put together the
following code. However, what should this.serverUrl be? Does this
need to be an ASP/PHP page capable of recieving a POSTed file or is
it a destination address on the server?

If the web server (and your overall architecture) allows HTTP PUT at the
destination URL, you don't need any server-side application code to
handle the upload.

Cheers,
 
Back
Top