Microsoft.SharePoint.Client 是一个用于与 SharePoint 网站进行交互的客户端对象模型库。它允许开发者通过编程方式执行各种操作,如上传文件、创建列表项、管理权限等。
以下是一个使用 Microsoft.SharePoint.Client 将图像上传到指定 SharePoint 文件夹的示例代码:
using System;
using Microsoft.SharePoint.Client;
class Program
{
static void Main()
{
string siteUrl = "https://your-sharepoint-site-url";
string username = "your-username";
string password = "your-password";
string folderServerRelativeUrl = "/Shared Documents/Images";
string localFilePath = @"C:\path\to\your\image.jpg";
using (ClientContext context = new ClientContext(siteUrl))
{
// 设置凭据
context.Credentials = new SharePointOnlineCredentials(username, password);
// 获取目标文件夹
Folder folder = context.Web.GetFolderByServerRelativeUrl(folderServerRelativeUrl);
context.Load(folder);
// 创建文件信息对象
FileCreationInformation fileInfo = new FileCreationInformation();
fileInfo.Overwrite = true;
fileInfo.Content = System.IO.File.ReadAllBytes(localFilePath);
fileInfo.Url = System.IO.Path.GetFileName(localFilePath);
// 添加文件到文件夹
Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(fileInfo);
context.Load(uploadFile);
// 执行请求
context.ExecuteQuery();
Console.WriteLine("文件已成功上传到: " + uploadFile.ServerRelativeUrl);
}
}
}
通过以上步骤和代码示例,你应该能够成功地将图像上传到 SharePoint 文件夹。如果在实际操作中遇到其他问题,建议查看 SharePoint 的日志文件或使用调试工具来进一步诊断问题所在。
没有搜到相关的文章