Sharepoint 2010使用Silverlight 4.0上传文件

时间:2011-07-17 11:16:54

标签: silverlight sharepoint-2010

我正在尝试从Silverlight(客户端对象模型)到Sharepoint 2010库进行文件上传..请参阅下面的代码..

        try{
            context = new ClientContext("http://deepu-pc/");
            web = context.Web;
            context.Load(web);
            OpenFileDialog oFileDialog = new OpenFileDialog();
            oFileDialog.FilterIndex = 1;
            oFileDialog.Multiselect = false;
            if (oFileDialog.ShowDialog().Value == true)
            {
                var localFile = new FileCreationInformation();
                localFile.Content = System.IO.File.ReadAllBytes(oFileDialog.File.FullName);
                localFile.Url = System.IO.Path.GetFileName(oFileDialog.File.Name);
                List docs = web.Lists.GetByTitle("Gallery");
                context.Load(docs);
                File file = docs.RootFolder.Files.Add(localFile);
                context.Load(file);
                context.ExecuteQueryAsync(OnSiteLoadSuccess, OnSiteLoadFailure);
            } 
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.ToString());
        }

但是我收到以下错误

System.Security.SecurityException: File operation not permitted. Access to path '' is denied.
   at System.IO.FileSecurityState.EnsureState()
   at System.IO.FileSystemInfo.get_FullName()
   at ImageUploadSilverlight.MainPage.FileUpload_Click(Object sender, RoutedEventArgs e)

任何帮助将不胜感激

由于

迪普

1 个答案:

答案 0 :(得分:3)

Silverlight运行时对客户端用户的文件系统的访问权限非常有限。使用打开文件对话框时,您可以在其父文件夹中获取所选文件的名称,文件的长度以及从中读取文件中数据的流,但不会多于此。您无法读取所选文件的完整路径,并且您将获得异常,因为您正在尝试这样做。

如果要将文件的整个内容读入字节数组,则必须替换该行

localFile.Content = System.IO.File.ReadAllBytes(oFileDialog.File.FullName);

类似

localFile.content = ReadFully(oFileDialog.File.OpenRead());

ReadFully方法将流的整个内容读入字节数组。它不是标准的Silverlight方法;相反 取自this answer。 (我在Silverlight上对此方法进行了快速测试,看起来很有用。)