My Silverlight series (4)-annoying Silverlight File Operations

Source: Internet
Author: User

Recently, my Silverlight project has the following requirement: click a control, Just Like clicking a hyperlink on HTML, and the browser prompts you to download a file, of course, the file URI is known and is handled by the event triggered by this click.ProgramCalculated.

 

To achieve this, I tried a lot of methods. Since Silverlight only supports isolation of the storage zone and it does not have controls similar to hyperlinks, I have to turn to JS and Asp.net. In an article I posted, the method was indeed brilliant, but the results of the experiment were quite unsatisfactory. Here I will simulate that implementation method and give the result.

 

In the Silverlight CS FileCodeAs follows:

 

Private   Void Button_click ( Object Sender, routedeventargs E)
{
String Uri_site =   " Http://www.cnblogs.com " ;
String Uri_file =   " Http://files.cnblogs.com/wodehuajianrui/BubblyStyleProject.zip " ;
Object [] ARGs =   {Uri_site, uri_file} ;
Htmlpage. Window. Invoke ( " Silverlightbutton_onclick " , ArgS );
Htmlpage. Window. Invoke ( " Setlinkhref " , ArgS );
}

The HTML code in aspx is as follows:

 

Div style = "margin-bottom: 100px">
< A ID = "Download" Href = "" Target = "_ Blank" Style = "Margin-Right: 58px" > Download a file </ A >
< A ID = "Open" Href = "" Target = "_ Blank" Style = "Margin-Right: 65px" > Open a site </ A >
< Input ID = "Button" Type = "Button" Value = "Download" Onclick = "Return button_onclick ()"   />
</ Div >

 

The JS Code in aspx is as follows:

 

< Script Language = " Javascript " Type = " Text/JavaScript " >

Function Button_onclick () {

Window. Open ("http://www.cnblogs.com", "_ blank ");
Window. Open ( " Http://files.cnblogs.com/wodehuajianrui/BubblyStyleProject.zip " , " _ Blank " );
}

Function Silverlightbutton_onclick () {
If (Arguments. Length >   0 ) {
Window. Open (arguments [0],"_ Blank");
Window. Open (arguments [1],"_ Blank");
}
}

Function Setlinkhref () {
If (Arguments. Length >   0 ) {
Open. href=Arguments [0];
Download. href=Arguments [1];
}
}

< / SCRIPT>

 

Explain the functions of these codes: first, I defined a button in the sliverlight XAML file, and its event processing code is shown in the first section, then I added a div TO THE ASPX page and added three elements for comparison. Running effect:

First, I click the button in Silverlight. The result is to open two pages, one is the homepage of the blog Garden and the other is the link to a file. the homepage of the blog garden is successfully opened and stuck, and the other window is automatically closed.

Then I click the button in aspx, and the result is to open two pages, one is the blog garden homepage, and the other is a file link. The blog garden homepage is successfully opened and stuck, another window prompts you to download the file.

Because you click the Silverlight button to assign values to the two links at the same time, the two links can also be opened normally, and the link pointing to the file prompts download.

 

Now the problem is that the js method is called using Silverlight. This open file link operation will be automatically canceled. Even if I directly call the button_click method in JS without passing parameters from Silverlight, the result is the same, and the new window that opens the file will be closed automatically without prompting for download.

 

Interestingly, if we change the code slightly, change the js method called by Silverlight:

Function Silverlightbutton_onclick () {
If (Arguments. Length >   0 ) {
Window. Open (arguments [0],"_ Blank");
Window. Open (arguments [1],"_ Self");
}
}

 

Opening the file in this window will make things different. :

After I allow the download, the page is refreshed. If I click the Silverlight button again, the file download prompt is displayed normally. Correspondingly, I also modify the response code of the button in aspx:

 

Function Button_onclick () {
Window. Open ("Http://files.cnblogs.com/wodehuajianrui/BubblyStyleProject.zip","_ Self");
}

 

The running result is as follows:

It is not blocked by the browser, but a normal prompt for download. It can be seen that sliverlight greatly limits its behavior for security reasons. At the same time, this is the result of my Js call. If I call Dom and ask the ASPX page to post a file to my browser, it will also be intercepted ...... Originally, we used sliverlight to improve user experience, but the page is frequently refreshed and always jumps out of the prompt, which makes people very uncomfortable...

 

I don't want to use opportunistic methods. Microsoft has made some tricks in SkyDrive in his MSN space. His is An ASPX page, however, if a file is post through the server, it will be intercepted by the browser. So Microsoft engineers have come up with a way to skillfully apply CSS, A link is dressed like a button, connected to the on: Focus pseudo class, and then dynamically assigned a value for the href of The Link Using Js, therefore, when you click the link that looks like a button, the file will be downloaded directly (because this is your own GET request, so the browser will not block it ). Therefore, in Silverlight, we can use a similar method to spoof users ....

 

Speaking of this, I hope Microsoft will consider removing these restrictions when developing the next version of Silverlight. This is so annoying. Since Silverlight can do something through JS, why don't we have it hosted. net code? We sincerely hope that we can find a 10 thousand strategy between convenience and security!

 

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.