views:

1349

answers:

11

I'm working on a Silverlight control that will allow multi-file downloading. At the moment I'm trying to get my mind around the permission model of the browser.

Let's say, on a web page, a user enters a local folder (c:\temp) in a text box. The user then clicks a button.

Is it possible in JavaScript or Silverlight, to write a collection of files (that are stored on the server) to that folder on the user's drive?

+11  A: 

From Javascript - NO. It would be way too easy from some scumbag to install a virus on your PC if that were possible.

Silverlight I don't know about, but I would assume writing to the users hard drive would be very limited and tightly controlled.

rikh
This is incorrect. You can do this using ActiveX in MSIE or a similar call in Firefox. In both cases the user will need to 'okay' a dialogue warning that the browser is requesting privileges that are dangerous.
username
To clarify, you can call file io directly in Javascript from both MSIE and Firefox. Someone's already posted a link here for MSIE. For Friefox, I don't have the details on me, but it's probably the same as it is using XUL. See this XUL example:http://www.captain.at/programming/xul/
username
+4  A: 

Only if the browser has a security hole that you can exploit.

Since the capability you describe would allow any webpage to do whatever it damn well pleases to the visitor's system, there is no way anyone would implement arbitrary access to the local disk deliberately in this day and age.

The next best thing you can to is to have the user download a ZIP archive and tell him to decompress him wherever he likes.

Michael Borgwardt
+5  A: 

You cannot from Silverlight. The only thing you have access to is the Isolated Storage.

http://blog.paranoidferret.com/index.php/2007/10/12/silverlight-tutorial-using-isolated-storage/

http://msdn.microsoft.com/en-us/library/bdts8hk0.aspx

Steven Behnke
A: 

I don't believe this is possible as it create countless security problems. The only way I can see this being secure is a mix of user permissions and some way of verifying (on the client side) the authenticity of the website and the files they want to download to my computer.

One method you could do is triggering the "FileSaveDialog" but I don't think that is what your looking for. Maybe packaging the collection of files first and then triggering is the way to achieve your goals. Silverlight only allows for 1mb of isolated storage per domain and will absolutely not give sites access to the clients hard drive.

Forrest Marvez
+3  A: 

I have used the Scripting.FileSystemObject to do this. Only works in IE, and only with very relaxed and unsafe security settings, but it might work for intranet sites in an enterprise, at least it worked for me.

erikkallen
useful info - thanks
Guy
+1  A: 

As it was answered earlier, this would have to be done via server-side, Silverlight and JavaScript are sandboxed to disable this type of security hole. I would suggest packing the files into a zip archive on the server and then downloading the zip archive.

Michael S. Scherotter
+1  A: 

You can do this via an ActiveX control or Java applet. Either way you do this the project has to be signed with a code signing certificate. You can use a self-signed certificate but the warning dialogs will be louder and brighter.

You can then use Javascript to call into the signed control and save files. Be very careful about creating an applet like this. You don't want to allow any other website to use this control so the control must implement some of the following:

  1. Only work on a certain website.
  2. Save files only in a specific location.
  3. Prompt the user where to save the files so that they know that this is happening and it doesn't happen silently in the background.

You can probably find a control or applet somewhere that can be controlled with Javascript.

sjbotha
+2  A: 

Google Gears has a method to do client-side storage.

http://gears.google.com/

That said, I doubt it will do what you are attempting. I think you would be better off having a server-side thing download files and put them into a .zip file for the client before passing it on to them.

Frakkle
+3  A: 

Most of these answers are incorrect. You can write to a file via Javascript in both MSIE (using ActiveX FileSystemObject) and Firefox (using nsIFileOutputStream). In both cases the user will be presented with a security dialogue.

username
A: 

This doesn't directly answer the question, but Adobe Flex 4 allows doing it with FileReference class.

OutputLogic
A: 

Silverlight 4 OOB allows access to the user's document directory.

Sky Sanders