Guest User

Untitled

a guest
Jul 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. REST WCF Service Code:
  2.  
  3. [OperationContract]
  4. [WebInvoke(UriTemplate = "uploadImage/{parameter1}")]
  5. void uploadImage(Stream fileStream);
  6.  
  7. public void uploadImage(Stream fileStream)
  8. {
  9. string filePath = @"C:ImageUpload";
  10. FileStream filetoUpload = new FileStream(filePath, FileMode.Create);
  11.  
  12. byte[] byteArray = new byte[10000];
  13. int bytesRead, totalBytesRead = 0;
  14.  
  15. do
  16. {
  17. bytesRead = fileStream.Read(byteArray, 0, byteArray.Length);
  18. totalBytesRead += bytesRead;
  19. }
  20. while (bytesRead > 0);
  21. filetoUpload.Write(byteArray, 0, byteArray.Length);
  22. filetoUpload.Close();
  23. filetoUpload.Dispose();
  24. }
  25.  
  26. protected void btnUpload_Click(object sender, EventArgs e)
  27. {
  28. string file = FileUpload1.FileName;
  29. RESTService1Client client = new RESTService1Client();
  30.  
  31. byte[] bytearray = null;
  32. string name = "";
  33. if (FileUpload1.HasFile)
  34. {
  35. name = FileUpload1.FileName;
  36. Stream stream = FileUpload1.FileContent;
  37. stream.Seek(0, SeekOrigin.Begin);
  38. bytearray = new byte[stream.Length];
  39. int count = 0;
  40. while (count < stream.Length)
  41. {
  42. bytearray[count++] = Convert.ToByte(stream.ReadByte());
  43. }
  44. }
  45. WebClient wclient = new WebClient();
  46. wclient.Headers.Add("Content-Type", "image/jpeg");
  47. client.uploadImage(FileUpload1.FileContent);
  48. }
  49.  
  50. [OperationContract]
  51. [WebInvoke(UriTemplate = "uploadImage/{parameter1}/{parameter2}")]
  52. void uploadImage(Stream fileStream, string fileName);
  53.  
  54. public void uploadImage(Stream fileStream, string fileName)
  55. {
  56. string filePath = @"C:ImageUpload";
  57. using (FileStream filetoUpload = new FileStream(filePath + fileName, FileMode.Create))
  58. {
  59. byte[] byteArray = new byte[10000];
  60. int bytesRead = 0;
  61.  
  62. do
  63. {
  64. bytesRead = fileStream.Read(byteArray, 0, byteArray.Length);
  65. if (bytesRead > 0)
  66. {
  67. filetoUpload.Write(byteArray, 0, bytesRead);
  68. }
  69. }
  70.  
  71. while (bytesRead > 0);
  72. }
  73. }
  74.  
  75. protected void btnUpload_Click(object sender, EventArgs e)
  76. {
  77. if (FileUpload1.HasFile)
  78. {
  79. RESTService1Client client = new RESTService1Client();
  80.  
  81. client.uploadImage(FileUpload1.FileContent, Path.GetFileName(FileUpload1.FileName));
  82. }
  83. }
Add Comment
Please, Sign In to add comment