wodehuajianrui

博客园 首页 新随笔 联系 订阅 管理

Saving a file to the local disk in Silverlight

All user code in Silverlight runs in a sandbox. Hence, for security reasons, there are no APIs to directly open local files from disk. However, there is a OpenFileDialog class which allows a Silverlight app to open files on disk. Human intervention is required to interact with the dialog box. This ensures that the user trusts the app to open the files selected in the dialog box.

However, currently there is no equivalent to writing to a file to disk, even if the user wants to allow such an operation. This is unfortunate. If you implemented a text or image editor in Silverlight, the user would not be able to save the edited file to disk. However, there is a workaround - but at the cost of extra network traffic. The solution involves the following steps:

  • The Silverlight client app running in the browser sends the data that needs to be saved to disk to the web server using a POST operation using System.Windows.Browser.Net.BrowerHttpWebRequest::GetResponse
  • The web server saves the data to a temporary file and returns a URL for the temporary file in the response
  • The Silverlight client app reads the URL that is returned in the System.Net.HttpWebResponse object.
  • It navigates to the URL using System.Windows.Browser.HtmlPage::Navigate.
  • This will result in the browser invoking the default action for the given file type. This is often to prompt the user to save the file to disk. If the browser opens the file directly, the user could then have the browser save the file to disk. If multiple files need to be saved, the web server could save them all in a zip file using System.IO.Compression.GZipStream.

This solution is obviously not ideal as it result in unnecessarily sending the data back and forth over the network. For large files, this could be prohibitive. For small files, its a reasonable solution until there is a SaveFileDialog API in Silverlight.

I do not know if there will be a SaveFileDialog in Silverlight 1.1, but I hear that the Silverlight team is looking into it...

 

来源:Shri Borde's Webblog

 

翻译如下

 

Silverlight的所有用户代码都是运行在沙盒之中的,因此出于安全因素考虑,并不存在直接打开本地文件的API。但是,OpenFileDialog控件允许Silverlight程序打开一个本地文件。在这个过程中,对话框需要有人为的操作,这保证了用户允许Silverlight程序打开对话框中的文件。

但是,即使用户想通过Silverlight向本地存储一个文件,至今也没有任何一个等效的控件支持这个操作。这是不合适的,设想你在Silverlight中编辑一个文本或者一张图片,用户会希望把已经编辑好的文件存储在本地。为了实现这个操作,有一种消耗很多网络流量的做法,它需要如下几个步骤:

  • 运行在客户浏览器上的Silverlight程序将需要存储在本地的数据通过POST操作传给web服务器,这将要使用System.Windows.Browser.Net.BrowerHttpWebRequest::GetResponse
  • web服务器将数据存储为一个临时文件并且返回一个URL
  • Silverlight程序在System.Net.HttpWebResponse对象中获取该URL
  • 通过System.Windows.Browser.HtmlPage::Navigate转向到那个URL
  • 浏览器将会行使它指向一个文件的默认行为——提示用户保存那个文件。如果有很多文件需要存储,web服务器需要通过System.IO.Compression.GZipStream将他们存储为一个ZIP文件。

这个解决方案显然不理想,因为它引起了并不需要的数据上传和下载操作。对于大文件,这将是禁止的。对于小文件,这也许是Silverlight开发出SaveFileDialog之前的一个合理的权益之计。

我不知道在Silverlight 1.1中会不会有SaveFileDialog,但是我听说Silverlight小组正在考虑这个问题。

 

译者的话:现在Silverlight已经出了Silverlight2Beta2,但是SaveFileDialog仍然没有影子,不知道微软什么时候能让这一操作变方便呢……

 

注:本文为本人自己翻译的,限于英文水平,难免有不到位的地方,请大家谅解! 

 

 

posted on 2008-07-26 16:36  花间蕊  阅读(1847)  评论(0编辑  收藏  举报