Is it possible to read files stored on a computer? - silverlight

I want to access a file in computer(c:\test.bin) and to read it as byte array .Is it possible in Windows phone .
Thanks and Regards
vaysage

You cannot access a file via any standard file I/O apis.
You can run a web server on that computer, make the file available via http and include the appropriate client access policy file in the web site. You can then download the file via WebClient using OpenReadAsync.

If you would like to upload something as a project asset, then you should force it to be a "content" from the properties panel and then access it using :
Uri uriMyFile = new Uri("test.bin",UriKind.relative);
StreamReader sr = new StreamReader((Application.GetResourcesStream(
uriMyFile)).Stream);

Related

open document in browser without generating file in server side

I have stored user document (ms word) in data store as blob object.
How do I open this in a browser? I can make it downloadable but I want user to view the document in browser before downloading it. how do I do that?
I think, to be able to open the file in browser by setting response headers (inline and content-type), the file should physically exist on a file location.
I don't have space to generate the file and then write to the browser. How do I handle it?
can I store them in google drive? does reading and uploading will be charged? any pointer to examples?
daoIntf=new DAOImpl();
document = daoIntf.getEntity("Document", id);
Blob docBlob=(Blob)document.getProperty("resume");
String fileName=(String)document.getProperty("fileName");
String contentType=(String)document.getProperty("contentType");
response.setContentType(contentType);
response.setHeader("Pragma" , "no-cache");
response.setHeader("Cache-Control" , "no-cache");
response.setDateHeader("Expires" , 0);
response.setHeader("Content-Disposition" , "inline;filename=\""+fileName+"\"");
ServletOutputStream out = response.getOutputStream();
out.write(docBlob.getBytes());
-thanks
Ma
If you have a document in the Blobstore, you simply need to provide a link to it (i.e. .getServingUrl() in Java). When a user clicks on that link, his browser will either offer to download it, or it will open it in the preview mode if this user has a plugin or extension that can do it.
There is an online Google Docs viewer that works on external documents: https://docs.google.com/a/iddiction.com/viewer?pli=1
Expose your documents via servlet using blobstoreService.serve(key, response) and then open them in the viewer.
http://docs.google.com/viewer?url=<url_of_your_document>&embedded=<true|false>

Get JSON distant file and write/store a file with angularjs

I search how to write a file with angularjs, after get it in json. I have already the json file in my folder, but with onclick, users can manually update it with the new content and overwrite the same file. How to write it ? (i don't ask the get distant file part).
I need locastorage functions ? it's for android application, so which storage i must use ?
You can't write files with javascript. You would need to pass the data to PHP or nodejs (aka a server side language) that has access to your filesystem. Javascript is client facing, so it doesn't know anything about the filesystem on the server it is running.

How to get file in Silverlight Application?

Suppose, we have a sivlerlight application - SLApp. SLApp is located on the WebSite someweb.com. Link on file likes this - someweb.com/SLApp?URL="https://smthg.com/file.exe" .
How to get this file in the silverlight app&
It's difficult to understand what you want from your question:
For getting a file from the client machine and sending it to the server:
You can't do that directly, it's a security risk, and hence disabled. You can only access files via the file open dialog.
For getting a file from the server to the client:
Now if you are wanting to download a file to the client via a link, then you can just open a web page with the link/url, that way you use the standard browser functionality.

Upload local database to SkyDrive as a textfile

I havent looked at the samples yet for the SkyDrive.
I have done a application that saves some data in a local database on the phone.
What i want to do is to upload all these to a SkyDrive account as a textfile.
Is this possible?
Might want to start with a blog post I wrote - Adding SkyDrive support to your Windows Phone application. You will need to download the Live SDK (unless you want to use the REST API manually, which I can see no reason for at this point).
Uploading is fairly simple after that, given that you obtained the proper session init:
client = new LiveConnectClient(App.MicrosoftAccountSession);
client.UploadCompleted += MicrosoftAccountClient_UploadCompleted;
client.UploadProgressChanged += MicrosoftAccountClient_UploadProgressChanged;
client.UploadAsync("me/skydrive", Binder.Instance.CurrentlyUploading,
stream, OverwriteOption.Overwrite);
Where stream is a MemoryStream instance.

System.IO.FileInfo throwing access is denied exception to a local file

I created a sample Silverlight Web project
and I am getting 'Access is denied' when I do this:
string fileName = "map.gif";
FileInfo fileInfo = new FileInfo(fileName);
How can I give the web project access to this folder/file?
I added the image into my project, really drawing a blank here....
You don't access files you've placed in the project using the FileInfo object. Instead you create a Uri to access it.
Its not clear from your question which project you've place the file in. If you have placed it in the Silverlight project then it ought to end up as content in the Xap. In which case you can acquire StreamResourceInfo for it using:-
StreamResourceInfo gifContentInfo = Application.GetResourceStream(new Uri("map.gif", UriKind.Relative));
Now you can get to the file content with:-
Stream gifStream = gifContentInfo.Stream;
On the other hand if you have placed the file in the web project it will be a standard static file in the web site. Hence you will need to do the typical WebClient download to fetch it.
I take it you are going to this trouble because its a Gif file; you are aware that they are not supported as an image.
You can't use the filesystem in Silverlight outside of Isolated Storage
you need to give file access to the asp.net user
check this out:
http://www.codeproject.com/KB/aspnet/Ahmed_Kader.aspx
Or use the special folder which asp.net provides for you
... APP_DATA
that should have the rights you need...
I am assuming you are trying to access a file in the local filesystem.
If so, you cannot access files like that. Silverlight does not have the access priveleges u expect. If you want to add a file to your Silverlight Application at runtime. You will need to have Silverlight 4, running Out of the Browser with Elevated priveleges. There are certain limitations to this too. You can only access files in Special Folders like My Documents, Pictures, Music etc. For more info about access files this way. You can look at John's tutorials on Silverlight 4 elevated priveleges in Channel 9 MSDN.
I would doubt your FileInfo usage too. Here is a sample code to get file data using a simple drag and drop feature.
private void list_Drop(object sender, DragEventArgs e)
{
FileInfo[] files = (FileInfo[])e.Data.GetData(DataFormats.FileDrop);
for(int i=0;i<files.Length;i++)
textblock.Text += files[i].Name;
}
You can get the properties of the file such as "Name". You wil not hit any access denied errors. You cannot access properties like "DirectoryName", "FullName" etc. The reason being they are declared as SecurityCritical properties for Security reasons. The advantage of elevated permissions is that you can get to local file system (special folders) to access the FullName and DirectoryName properties without any exceptions.
Hope this helps

Resources