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