Silverlight obtains the local physical path of the selected file,

Source: Internet
Author: User
Tags filetime domain hosting

Silverlight obtains the local physical path of the selected file,

Recently I was working on a large file resumable data transfer control. I tried to write pure C # code, but later I found that it was too inflexible, So I considered using the control. However, during control development, it is found that it is a big problem to obtain the physical path of the uploaded file, because Silverlight does not support obtaining the client path, not only Silverlight, all Microsoft upload controls do not support obtaining local physical paths. So I finally found a way to get the file information.

Let's look at an example.

1 # region select file 2 private void bt_SelectFile_Click (object sender, RoutedEventArgs e) 3 {4 OpenFileDialog ofd = new OpenFileDialog (); // initialize 5 ofd. multiselect = true; // You can select 6 ofd for multiple settings. filter = "select a file | '*'"; // restrict the file type and prompt 7 if (ofd. showDialog () = true) 8 {9 foreach (FileInfo file in ofd. files) 10 {11 UserFile userFile = new UserFile (); // custom file upload information Class 12 userFile. fileName = file. name; // get the file Name 13 userFile. fileStream = file. openRead (); // open the file 14 userFile in read-only mode. filePhysicalpath = file. fullName; // obtain the full path name of the directory or file 15 userFile when called by a trusted application. fileTime = file. lastWriteTime; // get the last modification date of the file 16} 17} 18} 19 # endregion when called by a trusted applicationSelect File

In fact, during debugging, we will find that userFile. filePhysicalpath = file. fullName; userFile. fileTime = file. lastWriteTime; the two sentences of code will report an error, prompting that the exception has not been debugged. This is because Silverlight does not allow access to client files under Microsoft's encapsulation. How can this problem be solved? In fact, it is very simple. In the latest Silverlight5 version, Microsoft allows developers to gain local file operation permissions by improving the application trust.

Right-click the properties of our Silverlight project. In the Silverlight column, we can see

Check [enhance trust when running in the browser], so that we can operate on local files!

Here, you need to explain the operations performed by Microsoft after the check box is selected, because sometimes we check the check box and still cannot get the file path.

When you select "enhance trust when running in a browser", visual studio will do the following:

1. Add the following content to the silverlight project file (. csproj): <RequireInBrowserElevation> true </RequireInBrowserElevation>

2. Add InBrowserSettings. xml to the Properties folder of the project.

3. Add the following content to the silverlight project file (. csproj): <InBrowserSettingsFile> Properties/InBrowserSettings. xml </InBrowserSettingsFile>

For more information, see my other article Silverlight controls-how to enhance application trust and solve problems

Now we can get the file path during debugging.

Okay. Let's send the project to the server and check it out. Why? Why cannot it be obtained? Originally, this was because the debugging was performed locally, but after the server was not sent, it was accessed through the Internet. That is to say, this was a cross-domain behavior. You must have come up with something smart. By the way, we can write a script that allows cross-origin access. It's actually very easy. Let's take a look at the following:

Create a clientaccesspolicy. xml file in our web project. The Code is as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <access-policy> 3 <cross-domain-access> 4 <policy> 5 <allow-from http-request-headers = "*"> 6 <domain uri = "* "/> 7 </allow-from> 8 <grant-to> 9 <resource path ="/"include-subpaths =" true "/> 10 </grant-to> 11 </policy> 12 </cross-domain-access> 13 </access-policy>Clientaccesspolicy. xml

Save the clientaccesspolicy. xml file to the root directory of the domain hosting the service. For example, if the service is hosted on a http://fabrikam.com, the file must be located at http://fabrikam.com/clientaccesspolicy.xml.

Create another crossdomain. xml with the following code:

1 <? Xml version = "1.0"?> 2 <! DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> 3 <cross-domain-policy> 4 <allow-http-request-headers-from domain = "*" headers = "*" /> 5 </cross-domain-policy>Crossdomain. xml

Store the crossdomain. xml file in the root directory of the domain hosting the service. For example, if the service is hosted on a http://fabrikam.com, the file must be located at http://fabrikam.com/crossdomain.xml.

For details, see make the service cross-domain boundary available.

Now we can open the website on another computer and enter the URL to get the path of our local file.




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.