Simple upload progress display through WCF File Upload

Source: Internet
Author: User
In many cases, uploading files is often used. Generally, I use two methods, one is through ashx extension, and the other is through WCF, this article only describes how to use the latter.

Practical functions: File Upload, simple upload progress display.

1. Create a new item in the Asp.net project: Silverlight-enabled WCF Service

Add a doupload method:

  1. 1: [servicecontract (namespace = "")]
  2. 2: [aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. Allowed)]
  3. 3: Public class service1
  4. 4 :{
  5. 5: [operationcontract]
  6. 6: Public void doupload (string filename, byte [] context, bool append)
  7. 7 :{
  8. 8: // upload directory
  9. 9: String folder = system. Web. Hosting. hostingenvironment. mappath ("~ /Upload ");
  10. 10: If (! System. Io. Directory. exists (folder ))
  11. 11 :{
  12. 12: // If the upload directory does not exist, create
  13. 13: system. Io. Directory. createdirectory (folder );
  14. 14 :}
  15. 15: // file read/write mode
  16. 16: filemode M = filemode. Create;
  17. 17: If (append)
  18. 18 :{
  19. 19: // If the parameter is true, the object is resumed and operated in append mode.
  20. 20: M = filemode. append;
  21. 21 :}
  22. 22:
  23. 23: // file write operation
  24. 24: Using (filestream FS = new filestream (Folder + @ "\" + filename, M, fileaccess. Write ))
  25. 25 :{
  26. 26: fs. Write (context, 0, context. Length );
  27. 27 :}
  28. 28: return;
  29. 29 :}
  30. 30 :}

Copy code

2. reference the previously completed WCF Service. Right-click the Silverlight project and choose "add service reference ".

Image.png(63.54 K) 21:40:10

Click the Discover button on the right.

Image_3.png(89.51 K) 21:40:10

Click "OK". Note: if an error occurs, press F5 to run the entire project.

3. After successful reference, the implementation code in Sl is introduced. For simplicity, I only use one button control:

Mainpage. XAML

  1. 1: <usercontrol X: class = "uploadfile. mainpage"
  2. 2: xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. 3: xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
  4. 4: xmlns: D = "http://schemas.microsoft.com/expression/blend/2008" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. 5: MC: ignorable = "D" D: designwidth = "640" D: designheight = "480">
  6. 6: <grid X: Name = "layoutroot">
  7. 7: <button X: Name = "BT" content = "Upload" Height = "100" width = "200"/>
  8. 8: </GRID>
  9. 9: </usercontrol>

Sponsors

4. mainpage. XAML. CS, all client operations

  1. 1: Public partial class mainpage: usercontrol
  2. 2 :{
  3. 3: Public mainpage ()
  4. 4 :{
  5. 5: initializecomponent ();
  6. 6 :}
  7. 7:
  8. 8: Public mainpage ()
  9. 9 :{
  10. 10: initializecomponent ();
  11. 11: // register the button click event
  12. 12: Bt. Click + = new routedeventhandler (bt_click );
  13. 13 :}
  14. 14:
  15. 15: // Click Event
  16. 16: void bt_click (Object sender, routedeventargs E)
  17. 17 :{
  18. 18: // select local file dialog box
  19. 19: openfiledialog d = new openfiledialog ();
  20. 20: // file filtering
  21. 21: D. Filter = "(*. *) | *.*";
  22. 22: // You can select only one file.
  23. 23: D. multiselect = false;
  24. 24: // The selection is complete.
  25. 25: If (D. showdialog () = true)
  26. 26 :{
  27. 27: // delete file email
  28. 28: fileinfo F = D. file;
  29. 29: // new instantiate a file from the uploadfile class
  30. 30: uploadfile file = new uploadfile ();
  31. 31: // file name
  32. 32: file. Name = f. Name;
  33. 33: // file stream content
  34. 34: stream S = f. openread ();
  35. 35: // file size (/1000 is used for convenient viewing and becomes k as the Unit)
  36. 36: file. size = S. Length/1000;
  37. 37: // instance file content
  38. 38: file. Context = new list <byte []> ();
  39. 39:
  40. 40: // read the content of the specified file stream to file. context to be uploaded.
  41. 41: int B;
  42. 42: While (S. Position>-1 & S. Position <S. length)
  43. 43 :{
  44. 44: If (S. Length-S. Position >=300)
  45. 45 :{
  46. 46: B = 3000;
  47. 47 :}
  48. 48: else
  49. 49 :{
  50. 50: B = (INT) (S. Length-S. position );
  51. 51 :}
  52. 52:
  53. 53: byte [] filebyte = new byte [B];
  54. 54: S. Read (filebyte, 0, B );
  55. 55: file. Context. Add (filebyte );
  56. 56 :}
  57. 57: S. Close ();
  58. 58:
  59. 59: // instantiate the WCF Client
  60. 60: servicereference1.uploadserviceclient Uploader = new uploadfile. servicereference1.uploadserviceclient ();
  61. 61: // register the doupload completion event
  62. 62: uploader. douploadcompleted + = new eventhandler <system. componentmodel. asynccompletedeventargs> (uploader_douploadcompleted );
  63. 63: // start uploading the first package
  64. 64: uploader. douploadasync (file. Name, file. Context [0], false, file );
  65. 65 :}
  66. 66 :}
  67. 67:
  68. 68: void uploader_douploadcompleted (Object sender, system. componentmodel. asynccompletedeventargs E)
  69. 69 :{
  70. 70: If (E. Error = NULL)
  71. 71 :{
  72. 72: // The previous package is successfully uploaded.
  73. 73: uploadfile file = E. userstate as uploadfile;
  74. 74: // modify the uploaded size (display function)
  75. 75: file. Sent + = file. Context [0]. Length/1000;
  76. 76: // Delete uploaded content
  77. 77: file. Context. removeat (0 );
  78. 78: // If the uploaded content is empty, complete the operation.
  79. 79: If (file. Context. Count = 0)
  80. 80 :{
  81. 81: Bt. content = "Upload ";
  82. 82: MessageBox. Show ("Upload OK ");
  83. 83 :}
  84. 84: else
  85. 85 :{
  86. 86: // If the content to be uploaded is not empty, continue uploading the remaining content.
  87. 87: (sender as servicereference1.uploadserviceclient). douploadasync (file. Name, file. Context [0], true, file );
  88. 88: // display progress
  89. 89: Bt. content = file. Sent. tostring () + "/" + file. Size. tostring ();
  90. 90 :}
  91. 91 :}
  92. 92 :}
  93. 93 :}
  94. 94:
  95. 95: public class uploadfile
  96. 96 :{
  97. 97: // file name
  98. 98: Public string name {Get; set ;}
  99. 99: // File Size
  100. 100: Public double size {Get; set ;}
  101. 101: // you have uploaded the file.
  102. 102: Public double sent {Get; set ;}
  103. 103: // upload content
  104. 104: public list <byte []> context {Get; set ;}
  105. 105 :}

Article reprinted: http://www.pin5i.com/showtopic-26069-2.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.