File operation not permitted in Silverlight? - 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.

Related

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

Image upload in window application ? C#.net?

In window application, i have one registration form in that one option is to upload the photo of the user. So how can i do this, generally in web application we have File Upload control using this we can save the image in our project, like that i want to save the photo of the user in any drive of my system. can you help me. Thank you.
You can use File Open Dialog Control. You can retrieve the physical path of the file and copy the file to your system specific location.
Sample:
string fulllocimage = openFileDialog1.FileName;
File.Copy(fulllocimage, Path.Combine(#"C:\UploadedImage\",Path.GetFileName(fulllocimage));

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").

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

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).

Opening an image on demand with Silverlight 2 WebClient

I'm trying to show some images on my silverlight application whenever user wants to. The images are in a folder in my silverlight project and I don't want the user to download all of them when he/she loads the web page for the first time.
I have tried the OpenReadAsync method with a relative address to the image file which is in a folder named images and its Build Action is set to Content and also its "Copy to Output Direcoty" property is set to Always.
But I get the following exception in the OpenReadCompleted event:
The URI prefix is not recognized.
Here is the code I used:
Dim webClient As New WebClient
AddHandler webClient.OpenReadCompleted, AddressOf webClient_OpenReadCompleted
WebClient.AllowReadStreamBuffering = True
WebClient.OpenReadAsync(New Uri("images/myimage.jpg", UriKind.Relative))
Can anyone tell me how can I solve this problem?
Thanks
To start with take the images out of the silverlight project. You want the images to be in the web project then you can use a normal image tag with an empty source, then when you need to download the image set the source to the uri.

Resources