Sharepoint 2010 使用 Silverlight 4.0 上传文件 [英] Sharepoint 2010 Upload file using Silverlight 4.0

查看:32
本文介绍了Sharepoint 2010 使用 Silverlight 4.0 上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 尝试{context = new ClientContext("http://deepu-pc/");web = context.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");上下文.加载(文档);文件文件 = docs.RootFolder.Files.Add(localFile);上下文.加载(文件);context.ExecuteQueryAsync(OnSiteLoadSuccess, OnSiteLoadFailure);}}捕获(异常 exp){MessageBox.Show(exp.ToString());}

但我收到以下错误

<前>System.Security.SecurityException:不允许文件操作.对路径 '' 的访问被拒绝.在 System.IO.FileSecurityState.EnsureState()在 System.IO.FileSystemInfo.get_FullName()在 ImageUploadSilverlight.MainPage.FileUpload_Click(Object sender, RoutedEventArgs e)

任何帮助将不胜感激

谢谢

深浦

解决方案

Silverlight 运行时对客户端用户的文件系统的访问非常受限.使用打开文件对话框时,您可以在其父文件夹中获取所选文件的名称、文件的长度以及从中读取文件中数据的流,但仅此而已.您无法读取所选文件的完整路径,并且由于您正试图准确地执行此操作而收到异常.

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

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

类似的东西

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

ReadFully 方法将流的全部内容读入字节数组.这不是标准的 Silverlight 方法;相反它摘自这个答案.(我在 Silverlight 上对该方法进行了快速测试,它似乎有效.)

I am trying to do a file upload from Silverlight(Client Object Model) to Sharepoint 2010 library.. Please see the code below..

        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());
        }

But I am getting the following error

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)

Any help would be appreciated

Thanks

Deepu

解决方案

Silverlight runs with very restricted access to the client user's filesystem. When using an open-file dialog, you can get the name of the selected file within its parent folder, the length of the file, and a stream from which to read the data in the file, but not much more than that. You can't read the full path of the file selected, and you are getting the exception because you are attempting to do precisely that.

If you want to read the entire content of the file into a byte array, you'll have to replace the line

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

with something like

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

The ReadFully method reads the entire content of a stream into a byte array. It's not a standard Silverlight method; instead it is taken from this answer. (I gave this method a quick test on Silverlight, and it appears to work.)

这篇关于Sharepoint 2010 使用 Silverlight 4.0 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆