How to upload files in js and asp.net that bypass the security restrictions of silverlight

Source: Internet
Author: User
Tags file upload silverlight

How to upload files through. net in js and asp tutorials that bypass the security restrictions of silverlight

1. The silveright page button calls the js code of the bearer page to open an aspx page for upload. All file uploads are completed on this page.

2. After the file is uploaded, the upload page will return the file name of the uploaded file to the bearer page, and then the bearer page will return to silverlight to complete other work.

The specific implementation is as follows:

1. First, this method requires silverlight to interact with the js code that hosts the page. Therefore, you must complete the following basic operations:

1 void FileUploader_Loaded (object sender, RoutedEventArgs e)
2 {
3 HtmlPage. RegisterScriptableObject ("FileUploadCtrl", this );
4}

 

 


Register this silverlight page as a script object in the loaded event of the page to be called, so that the js code in the page can use the method with the ScriptableMember feature in the background code on this page, follow these steps:

 

1 [ScriptableMember]
2 public void SetSelectedFile (string fullName)
3 {
4 SelectedFileName = fullName;
5}

 

 


This method returns the file name of the uploaded file to the JavaScript code that carries the page to silverlight.

Define the following functions in the bearer

 

1 function UploadFile (){
2 // open the file upload page
3 window. open ('fileupload. aspx ', 'upload attachment', 'height = 30, width = 300, toolbar = no, menubar = no, location = no, status = no ');
4}
5 function SetSelectedFile (fullname ){
6 // return the file name to silverlight
7 var control = document. getElementById ("SilverlightControl ");
8 var manager = control. Content. FileUploadCtrl;
9 manager. SetSelectedFile (fullname );
10}
11 function OpenAffix (msg ){
12 // open the file
13 if (arguments. length> 0 ){
14 window. open ("UploadFiles/" + arguments [0], "_ blank ");
15}
16}

 

 


2. After completing the above work, you can perform the following operations.

Put a button on the page and click the event to write it as follows:

 

1 public void Button_Click (object sender, RoutedEventArgs e)
2 {
3 try
4 {
5 ScriptObject sobjUploadFile = HtmlPage. Window. GetProperty ("UploadFile") as ScriptObject;
6 sobjUploadFile. InvokeSelf ();
7 return;
8}
9 catch (Exception ex)
10 {
11 MessageBox. Show (ex. Message );
12}
13}

 

 


The GetProperty parameter in row 5th is the name of the js function that carries the page definition. Run this code to call the UploadFile method of the page to open the FileUpload. aspx page.

3. The content of the FileUpload. aspx page is as follows:

 

1 <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "FileUpload. aspx. cs" Inherits = "OIU. Web. FileUpload" %>
 2
3 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4
5 6 7 <title> </title>
8 <script type = "text/webpage effects">
9 function SetSelectedFile (fullName ){
10 window. opener. location = "javascript: SetSelectedFile ('" + fullName + "');";
11 window. close ();
12}
13 </script>
14 15 <body>
16 <form id = "form1" runat = "server">
17 <div>
18 <asp: FileUpload runat = "server" ID = "fu"/>
19 & nbsp; <asp: Button Text = "upload" runat = "server" ID = "btnUpload"
20 onclick = "btnUpload_Clicked"/>
21 </div>
22 </form>
23 </body>
24

 

 


Note that the js function of Line 1 calls the js method of the parent page of the page through window. opener: SetSelectedFile.

The upload button click event handler function is written as follows:

 

1 protected void btnUpload_Clicked (object sender, EventArgs e)
2 {
3 if (fu. HasFile)
4 {
5 if (fu. PostedFile. ContentLength/1024/1024> 5)
6 {
7. ClientScript. registerStartupScript (ClientScript. getType (), "myscript", "<script> alert ('file upload is too large, up to 5 MB supported '); </script> ");
8 return;
9}
10 // string uploadFullName = string. Format ("{0} UploadFiles {1}", AppDomain. CurrentDomain. BaseDirectory, fu. FileName );
11 string uploadFullName = Server. MapPath ("~ /UploadFiles/") + fu. FileName;
12 fu. SaveAs (uploadFullName );
13 // return the full path of the file to the parent page
14 ClientScript. RegisterStartupScript (ClientScript. GetType (), "myscript", "<script> SetSelectedFile ('" + fu. FileName + "'); </script> ");
15}
16 else
17 {
18 ClientScript. RegisterStartupScript (ClientScript. GetType (), "myscript", "<script> alert ('select the file to be uploaded first '); </script> ");
19}
20}
21}

 

 


There are some verification operations. At first, I wanted to pass the uploaded full path to the page and then return it back to silverlight. However, when the front-end js function is called from the background, all the path separators of parameters are escaped, if anyone can solve this problem, follow the instructions to help me learn.

4. After completing the above operations, the file has been uploaded to the directory specified by the server. The next step is to download or open the uploaded file.

My implementation method is to use the silverlight page text click to call the OpenAffix function of the bearer page to open the file. The specific implementation is as follows:

 

1 private void StatusText_MouseLeftButtonUp (object sender, MouseButtonEventArgs e)
2 {
3 if (null! = StatusText. Tag &&! String. IsNullOrEmpty (StatusText. Tag. ToString ()))
4 {
5 HtmlPage. Window. Invoke ("OpenAffix", StatusText. Tag );
6}
7}

 

 


This is another method for silverlight to call the js function that carries the page. Refer to the js functions in article 1 to download or open files.

 

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.