Thursday, April 3, 2008

Upload image to web server using webservice .NET

To transfer a file to the web server, it is not necessary to use FTP to upload. You can use webservice to do so. Here is the trick:

public string CreateFileToBase64String(string path)
{
Stream fs = new FileStream(path, FileMode.Open);
MemoryStream ms = new MemoryStream();
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while ((numBytes = fs.Read(bytes, 0, size)) > 0)
ms.Write(bytes, 0, numBytes);
ms.Close();
fs.Close();
return Convert.ToBase64String(ms.GetBuffer());
}
public void CreateFileFromBase64String(string imageData, string path)
{
MemoryStream msf = new MemoryStream(Convert.FromBase64String(imageData));
Stream stem = new FileStream(path, FileMode.Create);
msf.WriteTo(stem);
msf.Close();
stem.Close();
}

you see things is so simple.

3 comments:

Anonymous said...

Excellent..
It works good

Owen Leong said...

thanks and cheers!

Chris Montiramalai said...

hello...i've been looking such software or shareware....but I don't know how it works with all these scripts, Owen.

I want to have something like Picasa or Flickr's Uploadr...coz I also enjoy taking photograph and I want to upload them to my own web server....but hmmm...maybe i'm too old for IT indeed..hehe

Could you tell me how all these scripts works and how can i use it?

Thanks