ASP. NET uploads files using Web Service

Source: Internet
Author: User

We know that Silverlight 2 provides a wide range of network communication APIs, including support for SOAP services, REST services, HTTP-based communication, and Socket communication. This article uses several examples to demonstrate how to upload files and send emails in Silverlight 2.

ASP. NET uploads files using Web Service

I will use an example to demonstrate how to use Web Service to upload files. First, I will create a Silverlight project and add an ASP. NET Web Service file to the Web test project. Now we can implement the relevant WebMethod. In this method, we will receive two parameters: byte array and file extension, and create a file on the server, as shown in the following code:

 
 
  1. C#   
  2. [WebMethod]   
  3. public int UploadFile(byte[] FileByte, String FileExtention)   
  4. {   
  5. FileStream stream = new FileStream(String.Format(@"D:\example.{0}", 
    FileExtention),FileMode.CreateNew);   
  6. stream.Write(FileByte, 0, FileByte.Length);   
  7. stream.Close();   
  8. return FileByte.Length;   
  9. }  

Add a simple interface for users to select a local file. We will click the button to call Web Service in the event, as shown in the following code:
XAML

 
 
  1. <Canvas Background="#FF333333">   
  2. <TextBox x:Name="txtFile"></TextBox>   
  3. <Button x:Name="btnUpload" Click="OnUploadClick"></Button>   
  4. <TextBlock x:Name="tblStatus"></TextBlock>   
  5. </Canvas>  


ASP.. NET calls Web Service to upload files. The OpenFileDialog object pop-up window is used to select files. This object returns the selected files as Stream, the following code converts Stream into a byte and passes it to Web Service:

 
 
  1. VoidOnUploadClick (objectsender, RoutedEventArgse)
  2. {
  3. OpenFileDialogopenFile=NewOpenFileDialog();
  4.  
  5. If (openFile. ShowDialog () = DialogResult. OK)
  6. {
  7. StringfileName=OpenFile. SelectedFile. Name;
  8.  
  9. FileServiceSoapClientclient=NewFileServiceSoapClient();
  10. Client. UploadFileCompleted + = newEventHandler<UploadFileCompletedEventArgs>
    (OnUploadFileCompleted );
  11.  
  12. Streamstream= (Stream) openFile. SelectedFile. OpenRead ();
  13. Stream. Position=0;
  14. Byte []Buffer=Newbyte[Stream. Length + 1];
  15. Stream. Read (buffer, 0, buffer. Length );
  16. StringfileExtention=FileName. Substring (fileName. IndexOf ('.') + 1 );
  17.  
  18. Client. UploadFileAsync (buffer, fileExtention );
  19. }
  20. }
  21.  
  22. VoidOnUploadFileCompleted (objectsender, UploadFileCompletedEventArgse)
  23. {
  24. If (E. Error= Null)
  25. {
  26. TblStatus. Text="File Uploaded! ";
  27. }
  28. }
  1. XML and ASP. NET
  2. Java script in ASP. NET calls the c # Method
  3. Process of processing ASP. NET Postback Program
  4. ASP. NET Server-side control CheckBoxList
  5. Analysis of ASP. NET Membership

Related Article

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.