dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » WCF »

Using wcf how to upload a image


Posted Date: 19 Jun 2012      Posted By:: shiva     Member Level: Bronze    Member Rank: 2981     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: 176     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
http://www.planetofcoders.com/



 
#676807    Author: Ajesh Madhukar Dalvi      Member Level: Silver      Member Rank: 760     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:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.