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: [servicecontract (namespace = "")]
- 2: [aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. Allowed)]
- 3: Public class service1
- 4 :{
- 5: [operationcontract]
- 6: Public void doupload (string filename, byte [] context, bool append)
- 7 :{
- 8: // upload directory
- 9: String folder = system. Web. Hosting. hostingenvironment. mappath ("~ /Upload ");
- 10: If (! System. Io. Directory. exists (folder ))
- 11 :{
- 12: // If the upload directory does not exist, create
- 13: system. Io. Directory. createdirectory (folder );
- 14 :}
- 15: // file read/write mode
- 16: filemode M = filemode. Create;
- 17: If (append)
- 18 :{
- 19: // If the parameter is true, the object is resumed and operated in append mode.
- 20: M = filemode. append;
- 21 :}
- 22:
- 23: // file write operation
- 24: Using (filestream FS = new filestream (Folder + @ "\" + filename, M, fileaccess. Write ))
- 25 :{
- 26: fs. Write (context, 0, context. Length );
- 27 :}
- 28: return;
- 29 :}
- 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: <usercontrol X: class = "uploadfile. mainpage"
- 2: xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- 3: xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
- 4: xmlns: D = "http://schemas.microsoft.com/expression/blend/2008" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
- 5: MC: ignorable = "D" D: designwidth = "640" D: designheight = "480">
- 6: <grid X: Name = "layoutroot">
- 7: <button X: Name = "BT" content = "Upload" Height = "100" width = "200"/>
- 8: </GRID>
- 9: </usercontrol>
4. mainpage. XAML. CS, all client operations
- 1: Public partial class mainpage: usercontrol
- 2 :{
- 3: Public mainpage ()
- 4 :{
- 5: initializecomponent ();
- 6 :}
- 7:
- 8: Public mainpage ()
- 9 :{
- 10: initializecomponent ();
- 11: // register the button click event
- 12: Bt. Click + = new routedeventhandler (bt_click );
- 13 :}
- 14:
- 15: // Click Event
- 16: void bt_click (Object sender, routedeventargs E)
- 17 :{
- 18: // select local file dialog box
- 19: openfiledialog d = new openfiledialog ();
- 20: // file filtering
- 21: D. Filter = "(*. *) | *.*";
- 22: // You can select only one file.
- 23: D. multiselect = false;
- 24: // The selection is complete.
- 25: If (D. showdialog () = true)
- 26 :{
- 27: // delete file email
- 28: fileinfo F = D. file;
- 29: // new instantiate a file from the uploadfile class
- 30: uploadfile file = new uploadfile ();
- 31: // file name
- 32: file. Name = f. Name;
- 33: // file stream content
- 34: stream S = f. openread ();
- 35: // file size (/1000 is used for convenient viewing and becomes k as the Unit)
- 36: file. size = S. Length/1000;
- 37: // instance file content
- 38: file. Context = new list <byte []> ();
- 39:
- 40: // read the content of the specified file stream to file. context to be uploaded.
- 41: int B;
- 42: While (S. Position>-1 & S. Position <S. length)
- 43 :{
- 44: If (S. Length-S. Position >=300)
- 45 :{
- 46: B = 3000;
- 47 :}
- 48: else
- 49 :{
- 50: B = (INT) (S. Length-S. position );
- 51 :}
- 52:
- 53: byte [] filebyte = new byte [B];
- 54: S. Read (filebyte, 0, B );
- 55: file. Context. Add (filebyte );
- 56 :}
- 57: S. Close ();
- 58:
- 59: // instantiate the WCF Client
- 60: servicereference1.uploadserviceclient Uploader = new uploadfile. servicereference1.uploadserviceclient ();
- 61: // register the doupload completion event
- 62: uploader. douploadcompleted + = new eventhandler <system. componentmodel. asynccompletedeventargs> (uploader_douploadcompleted );
- 63: // start uploading the first package
- 64: uploader. douploadasync (file. Name, file. Context [0], false, file );
- 65 :}
- 66 :}
- 67:
- 68: void uploader_douploadcompleted (Object sender, system. componentmodel. asynccompletedeventargs E)
- 69 :{
- 70: If (E. Error = NULL)
- 71 :{
- 72: // The previous package is successfully uploaded.
- 73: uploadfile file = E. userstate as uploadfile;
- 74: // modify the uploaded size (display function)
- 75: file. Sent + = file. Context [0]. Length/1000;
- 76: // Delete uploaded content
- 77: file. Context. removeat (0 );
- 78: // If the uploaded content is empty, complete the operation.
- 79: If (file. Context. Count = 0)
- 80 :{
- 81: Bt. content = "Upload ";
- 82: MessageBox. Show ("Upload OK ");
- 83 :}
- 84: else
- 85 :{
- 86: // If the content to be uploaded is not empty, continue uploading the remaining content.
- 87: (sender as servicereference1.uploadserviceclient). douploadasync (file. Name, file. Context [0], true, file );
- 88: // display progress
- 89: Bt. content = file. Sent. tostring () + "/" + file. Size. tostring ();
- 90 :}
- 91 :}
- 92 :}
- 93 :}
- 94:
- 95: public class uploadfile
- 96 :{
- 97: // file name
- 98: Public string name {Get; set ;}
- 99: // File Size
- 100: Public double size {Get; set ;}
- 101: // you have uploaded the file.
- 102: Public double sent {Get; set ;}
- 103: // upload content
- 104: public list <byte []> context {Get; set ;}
- 105 :}
Article reprinted: http://www.pin5i.com/showtopic-26069-2.html