You must Sign In to post a response.

Using wcf how to upload a image

using wcf/wcf web services to upload a images give me with example?


Comments

Author: Gaurav Agrawal19 Jun 2012 Member Level: Gold   Points : 3

The easiest way is to convert the image to a byte array before sending it, and then converting it back to an image on the destination site.

Here are two methods for doing just that:

public byte[] ImageToByteArray( Image image)
{
var ms = new MemoryStream();
image.Save(ms, ImageFormat.Png);
return ms.ToArray();
}

public static Image ByteArrayToImage(byte[] byteArray)
{
var ms = new MemoryStream(byteArray);
return Image.FromStream(ms);
}
That means your web service can have a method something like this:

public void UploadImage( byte[] imageData )
{
var image = ByteArrayToImage( imageData );
//save image to disk here, or do whatever you like with it
}

Author: Ajesh Madhukar Dalvi22 Jun 2012 Member Level: Silver   Points : 4

[OperationContract]
[WebInvoke(UriTemplate = "uploadImage/{parameter1}/{parameter2}")]
void uploadImage(Stream fileStream, string fileName);

.

public void uploadImage(Stream fileStream, string fileName)
{
string filePath = @"C:\ImageUpload\";
using (FileStream filetoUpload = new FileStream(filePath + fileName, FileMode.Create))
{
byte[] byteArray = new byte[10000];
int bytesRead = 0;

do
{
bytesRead = fileStream.Read(byteArray, 0, byteArray.Length);
if (bytesRead > 0)
{
filetoUpload.Write(byteArray, 0, bytesRead);
}
}

while (bytesRead > 0);
}
}

and your client side as such:

protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
RESTService1Client client = new RESTService1Client();

client.uploadImage(FileUpload1.FileContent, Path.GetFileName(FileUpload1.FileName));
}
}