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

Re: Uploadng Files to a Web Server by Jon

Jon
Thu Jul 24 08:51:13 CDT 2003

Derek Lakin <derek_lakin@lineone.net> wrote:
> 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);


--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Re: Uploadng Files to a Web Server by Derek

Derek
Thu Jul 24 09:12:51 CDT 2003

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" <skeet@pobox.com> wrote in message
news:MPG.1989f4b03f524bcf98a1e0@news.microsoft.com...
> Derek Lakin <derek_lakin@lineone.net> wrote:
> > 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);
>
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet/
> If replying to the group, please do not mail me too



Re: Uploadng Files to a Web Server by Joerg

Joerg
Sat Jul 26 05:57:03 CDT 2003

"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,
--
Joerg Jooss
joerg.jooss@gmx.net