gpt4 book ai didi

c# - 如何上传文件到sharepoint文档库

转载 作者:太空狗 更新时间:2023-10-30 01:21:21 25 4
gpt4 key购买 nike

谁能建议我创建一个关于如何将文件上传到共享点文档库的 silver-light 沙盒解决方案。我是 C# 开发人员。

最佳答案

这里很难描述。但我假设你会得到 FileInfo从文件。对于 silver light 应用程序,您需要添加两个客户端对象模型的引用 dll。

Microsoft.SharePoint.Client.Silverlight.dll.
Microsoft.SharePoint.Client.Silverlight.Runtime.dll.

您可以在 C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin 目录路径下找到上述 dll。

 private ClientContext context;
private Web web;

private void UploadFile(FileInfo fileToUpload, string libraryTitle, string subfolderPath, bool fileOverwrite)
{
try
{
//Treatment of files and loading it to byte array []
Stream str = null;
Int32 strLen, strRead;

str = fileToUpload.OpenRead();
strLen = Convert.ToInt32(str.Length);

byte[] strArr = new byte[strLen];
strRead = str.Read(strArr, 0, strLen);

using (context = new ClientContext("http://localhost/"))
{
web = context.Web;

//Defining where to find the files to tape record the library go
List destinationList = web.Lists.GetByTitle(libraryTitle);

//Creating a file
var fciFileToUpload = new FileCreationInformation();
fciFileToUpload.Content = strArr;

//Must determine whether to upload files directly to the library or whether to upload the files to sub directories and stack your way to the file
string uploadLocation = fileToUpload.Name;

if (!string.IsNullOrEmpty(subfolderPath))
{
uploadLocation = string.Format("{0}/{1}", subfolderPath, uploadLocation);
}
uploadLocation = string.Format("{0}/{1}/{2}", webUrl, libraryTitle, uploadLocation);

//Sets the path to the file where you want to upload and subor whether to overwrite the file or not
fciFileToUpload.Url = uploadLocation;
fciFileToUpload.Overwrite = fileOverwrite;

clFileToUpload = destinationList.RootFolder.Files.Add(fciFileToUpload);

//load web,list.
context.Load(web);
context.Load(destinationList, list => list.ItemCount);
context.Load(clFileToUpload);
context.Load(clFileToUpload.ListItemAllFields);
context.ExecuteQueryAsync(OnLoadingSucceeded, OnLoadingFailed);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
unhideComponents();
}
}
private void OnLoadingSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(fileUploaded); // fileUploaded is function
}

private void OnLoadingFailed(object sender, ClientRequestFailedEventArgs args)
{
Dispatcher.BeginInvoke(fileNotUploaded); //fileNotUploaded is function
}

关于c# - 如何上传文件到sharepoint文档库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15917354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com