Forums » .NET » WCF »

Using wcf how to upload a image


Posted Date: 19 Jun 2012      Posted By:: shiva     Member Level: Bronze    Member Rank: 3096     Points: 1   Responses: 2



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



Responses

#676207    Author: Gaurav Agrawal      Member Level: Gold      Member Rank: 174     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     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
}

Thanks & Regards,
Gaurav Agrawal


 
#676807    Author: Ajesh Madhukar Dalvi      Member Level: Silver      Member Rank: 1944     Date: 22/Jun/2012   Rating: 2 out of 52 out of 5     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));
}
}


 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Update Service Reference
Previous : Difference wcf and wcf web service?
Return to Discussion Forum
Post New Message
Category: WCF

Related Messages

Subscribe to Subscribers
Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India