Silverlight 4: Read in file from local system without being OOB? - silverlight

Any tips/tricks on how to read a file from the local system dynamically in silverlight 4 without having to be out of browser?
Impersonation? Toggling app elevated trust on/off programmatically?
Or is this simply impossible to do without being out of browser?
As it stands I have a Pegasus ImageGear PDF viewer that I feed a "LoadDocument" method a stream of a PDF file.
This of course works fine if the file is an application resource and compiled with the application.
StreamResourceInfo resource = Application.GetResourceStream(new Uri("/TestRIA;component/SampleData/test.pdf", UriKind.Relative));
docViewer.LoadDocument(resource.Stream);
This silverlight application will be hosted through a website deployed on a server. This server has a partition specifically for repositories of files. These files in the "D:" partition are currently accessed by an ASPX web application and displayed in a PDF viewer. We're moving to silverlight, so as the user selects the grid row representation of that file in the repository, I know the "NAME" of the file. The repository's location is a string held in the database configured in another application. I simply concatenate the file name to that repository path and have the filepath.
Again, the 3rd party viewer's "LoadDocument" method has two overloads. One that accepts a stream of the PDF and one that accepts the filename of the PDF.
For example I have a click event that feeds the name of the document, and I already have the root path to concatenate it to:
void testButton_Click(object sender, EventArgs e)
{
string docName = myListBox.SelectedItem.Content.ToString();
docViewer.LoadDocument(repositoryPath + docName);
//OR using stream
Stream s = new FileStream(repositoryPath + docName, FileMode.Open);
docViewer.LoadDocument(s);
}

You cannot programatically interact with an arbitrary file in SL4. Period. There's your section of isolated storage you can read from and write to files, but that isn't what you're looking for. You can read and write files through the file open dialog, but again I think that's not what you want.
The only way out of the Silverlight sandbox is the network. You have to have to talk to a non-sandboxed service to do this. SL has OK support for Web Services, Http, and even sockets. This seems doable for you since you're talking about the file being somewhere on a "server".

I remember reading that this will work without elevated trust only if the code is initiated with a user action such as button click.
http://msdn.microsoft.com/en-us/library/ff382752%28v=vs.95%29.aspx
For security purposes, if a Silverlight application is a sandboxed
application, file and print dialog boxes must be user-initiated. This
means you must show them from a user-initiated action, such as the
click event handler for a button.
As a possible workaround, if your Silverlight app is backed by a service, you could make the reading/writing of the file be handled by the service, assuming it has access to the location(s) and sufficient rights in the destination folder(s).

Create an OpenFileDialog box and you can return stream(s) to the selected file(s).

Related

How to download file in server?

I want to download some text file from server, and I found component 'FileDownloader' in Vaadin.
However, I cannot find some examples. All examples are using outputStream.
FileDownloader takes a Resource and there is a subclass FileResource for your case (from the docs):
FileResources are files or directories on local filesystem. The files and directories are served through URI:s to the client terminal and thus must be registered to an URI context before they can be used. The resource is automatically registered to the application when it is created
First of all a FileDownloader needs a FileResource, so you should do something like
FileResource fr = new FileResource(new File("path/to/your/file/on/server"));
FileDownloader fileDownloader = new FileDownloader(fr);
After that you have to associate the FileDownload to a component, like, for example, a button
Button buttonDownload = new Button("Download", FontAwesome.DOWNLOAD);
fileDownloader.extend(buttonDownload);
This basically adds to the component an extension, which is, to keep it simple, a component-independent entity that enables easy comunication between client and server.
When you'll click the button, the extension will activate and will start the download.
Tweak the example to your needs. Cheers.

Access real directory path from SaveFileDialog in Silverlight

I have an issue with Silverlight application and SaveFileDialog.
Basically I'm using SaveFileDialog in order to get from user path where he wants to save file and what is the file name. Then I'm passing that path to an API of other application(I'm using Silverlight 5 in Elevated Trust mode) which creates file for me.
Problem arise when user uses IE8 in Protected Mode.
SaveFileDialog.SafeFileName returns just a name of the file, not the path, and when I try to get that path with FileInfo, I'm getting always desktop no matter what user has selected.
I tried different approaches, including write just one byte into the file using stream from the SaveFileDialog.OpenFile - no success
I tried to access private member of SaveFileDialog.File.DirectoryName using reflection - property is marked with [SecurityCritical] so I cannot access...
I'm planning now to try invoke native browser save dialog using js in order to accomplish this but I'm not sure it will be different than Silverlight dialog.
Anyone out there smarter or more experienced than me around this problem? :)
Thanks

Silverlight and XtraReports - opening generated PDF report in new tab in browser

I'm trying to send a PDF file from a WCF to silverlight client. PDF is generated by DevExpress XtraReports (in method XtraReport CreateReport(string reportTypeName, RootGenericReportParameterContainer reportInformation)).
Acually PDF is saved somewhere on clients computer after choosing save path in file save dialog - DevExpress takes care of everything - but I don't have a clue how to open the PDF in new tab in browser.
And here is another problem. Silverlight 4 has no access to local file system right? So information about local PDF location is useless. Maybe it would be better to save the PDF in WCF and send a link to it to the client - but how?
I would first question why you need to send the file to the Silverlight client. Get rid of that requirement and the solution becomes much simpler. Silverlight can provide a link that opens a new browser tab. That link would be handled by the web domain, processing it as an HttpHandler, generating the PDF file for the browser. Your PDF url doesn't have to reference a physical file, you can still generate it on request, handle querystring values, etc... Lots of different ways to do this.
Seems that the question isn't really about DevExpress or Silverlight - you're just looking to open a [document of some kind] in a new tab. Each browser natively handles things differently, and users can change tab handling to whatever they want. And (as you mentioned) once the user has downloaded the file, you no longer control it.
Your best bet (and the way I do it) is probably to have a link pointing to a handler/file using "target='_blank' " in the anchor tag on the webpage. From the server side, you would want to set the "Content-Disposition" header to "Inline" to indicate to the browser that the document should be displayed in place instead of downloaded ("Attachment").

File operation not permitted in Silverlight?

I have silverlight application in asp.net web. In web application i have folder which contains XPS file. Now In silverlight on button click event I am opening a popup box which contains XPS Viewer.
On button click event i'am sending URI to public function of Popup control.
and i am trying to create filestream from that uri path but getting error
File operation not permitted Access to path "" denied ?
Using AbsolutePath m trying to creat file stream
From which property of URI object i should try to create FileStream or how do i achieve it?
Creating a filestream from there will not be permissible as silverlight is in a sandboxed environment. If you need a stream open the the file first with webclient and OpenReadAsync and then use the stream from the result.
Have a look at the answer here to see more or less how to do it.
You might also be interested in this link.

store image in a folder form a list in silverlight

I want to store a image in to a folder which I have selected from a list in silverlight
In Silverlight you have only limited access to the file system for security reason.
So everything what you want to save would end up in IsolatedStorage...
Check out this Quickstart Video and let me know if it helps
http://www.silverlight.net/learn/quickstarts/isolatedstorage/
In Silverlight, You have limited access to the client file system. If you are running Out Of Browser application with elevated permission, you can access User folders (My documents in windows).
But you can try some workarounds like using JavaScript u can try to download file. For reference Download a picture OnClick

Resources