Silverlight utility tip series: 19. Silverlight calls WebService to upload multiple files [with source code instance]

Source: Internet
Author: User

Silverlight does not support reading and writing server-side hard disks, but many users need to upload files to the server on the Silverlight client. This section describes the simplest way to upload files in Silverlight: serialize the file to a byte [] character group on the Silverlight client. Then, the server uses WebService to receive the data from the client and restore it to a file.

Next we will create a Silverlight application solution slupload, and right-click this project in the slupload. Web project to add a Web Service Page upload. Write the following code on this page:

/// <Summary>
/// Upload a file
/// </Summary>
/// <Param name = "filebyte"> file byte </param>
/// <Param name = "FILENAME"> file name </param>
/// <Param name = "fileextention"> file extension </param>
/// <Param name = "savepath"> file storage address </param>
/// <Returns> </returns>
Public String uploadfile (byte [] filebyte, string filename, string savepath)
// File storage location
String filepath = savepath + filename;
// Determine whether the same file exists at this location. If yes, delete the file with the same name.
If (file. exists (filepath ))
// Delete an object of the same name in the specified location
File. Delete (filepath );
// Create a filestream instance based on the incoming byte [] data and file location
Using (filestream stream = new filestream (filepath, filemode. createnew ))
// Write data streams to the file
Stream. Write (filebyte, 0, filebyte. Length );
Return filename;

The parameters of this web service method represent the transmitted file content byte group, file name, and file storage path. The Operations used in the method are described in the source code above, so I will not talk about them much. In the slupload project, right-click the project name, click "add service reference", and enter slupload. web upload. enter the URL of the asmx page and the namespace as uploadservice. Click OK to reference the WebService IN THE slupload project. The mainpage. XAML code is as follows:

<Grid X: Name = "layoutroot" background = "white">
<Button content = "Upload" Height = "23" horizontalalignment = "Left" margin = "20, 36, 0, 0 "name =" btnupload "verticalignment =" TOP "width =" 75 "Click =" btnupload_click "/>
<ListBox Height = "183" horizontalalignment = "Left" margin = "112,36, 232" name = "listbox1" verticalignment = "TOP" width = ""/>
<Button content = "" Height = "23" horizontalalignment = "Left" margin =, 0, 0 "name =" button1 "verticalignment =" TOP "width =" 75 "Click =" button#click "/>

Next, let's take a look at the key code of mainpage. XAML. cs. Pay attention to the reference here: using system. IO; using slupload. uploadservice; the two domain name spaces:

Private void btnupload_click (Object sender, routedeventargs E)
// Set an instance of the window for selecting files and set it to multiple
Openfiledialog dialog = new openfiledialog ();
Dialog. multiselect = true;
If (dialog. showdialog (). value)
// Obtain multiple selected files cyclically and upload these sets by calling WebService
Foreach (fileinfo file in dialog. files)
// Read stream
Stream stream = file. openread ();
Stream. Position = 0;
// Set byte array Initialization
Byte [] buffer = new byte [stream. Length + 1];
// Write the file to the byte array
Stream. Read (buffer, 0, buffer. Length );
String filename = file. Name;
// Call the WebService method to upload a file
Uploadsoapclient upfile = new uploadsoapclient ();
// Set the characters, file names, and file storage path for buffer access, and call the upload Method
Upfile. uploadfileasync (buffer, filename, "C :\\");
Upfile. uploadfilecompleted + = new eventhandler <uploadfilecompletedeventargs> (upfile_uploadfilecompleted );
MessageBox. Show ("You have not selected a file. Please select another file! ");
Void upfile_uploadfilecompleted (Object sender, uploadfilecompletedeventargs E)
This. listbox1.items. Add (E. Result + "the file is uploaded successfully! ");

Private void button#click (Object sender, routedeventargs E)
This. listbox1.items. Clear ();

This is the simplest Silverlight upload method, but in actual operation we can see that this upload method can only upload files smaller than 2 MB. This is because maxbuffersize = "2147483647" is set in the WebService transmission configuration of the servicereferences. clientconfig file ". Obviously, if we want to transfer large files, we need to consider cutting a large file into several small files for transmission. This is the next topic.

This example is written in vs2010 + Silverlight 4.0. Click slupload.rar to download the source code instance.

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: 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.