要将文件上传到SharePoint中的文档库,您可以按照以下步骤操作:
如果您需要上传多个文件,可以使用“上传多个文件”功能。只需要在步骤3中选择“上传多个文件”选项,然后按照提示操作即可。
如果您需要将文件上传到SharePoint中的特定文件夹,可以先导航到该文件夹,然后按照上述步骤上传文件。
如果您需要使用编程方式上传文件到SharePoint中的文档库,可以使用SharePoint的CSOM(客户端对象模型)或REST API。以下是使用CSOM上传文件的示例代码:
using System;
using System.IO;
using Microsoft.SharePoint.Client;
class Program
{
static void Main(string[] args)
{
string siteUrl = "https://your-domain.sharepoint.com/sites/your-site";
string username = "your-username";
string password = "your-password";
string filePath = @"C:\path\to\your\file.txt";
string libraryName = "Documents";
ClientContext clientContext = new ClientContext(siteUrl);
SecureString passwordSecureString = new SecureString();
foreach (char c in password)
{
passwordSecureString.AppendChar(c);
}
clientContext.Credentials = new SharePointOnlineCredentials(username, passwordSecureString);
List documentLibrary = clientContext.Web.Lists.GetByTitle(libraryName);
FileCreationInformation fileCreationInformation = new FileCreationInformation();
fileCreationInformation.Content = System.IO.File.ReadAllBytes(filePath);
fileCreationInformation.Url = Path.GetFileName(filePath);
Microsoft.SharePoint.Client.File uploadedFile = documentLibrary.RootFolder.Files.Add(fileCreationInformation);
clientContext.Load(uploadedFile);
clientContext.ExecuteQuery();
}
}
在上述代码中,您需要将siteUrl
、username
、password
、filePath
和libraryName
变量替换为您的SharePoint网站URL、用户名、密码、要上传的文件路径和文档库名称。
如果您需要使用REST API上传文件,可以使用以下代码:
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
class Program
{
static void Main(string[] args)
{
string siteUrl = "https://your-domain.sharepoint.com/sites/your-site";
string username = "your-username";
string password = "your-password";
string filePath = @"C:\path\to\your\file.txt";
string libraryName = "Documents";
string accessToken = GetAccessToken(siteUrl, username, password);
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
using (HttpResponseMessage response = httpClient.GetAsync($"{siteUrl}/_api/web/lists/getbytitle('{libraryName}')/RootFolder?$select=ServerRelativeUrl").Result)
{
string serverRelativeUrl = response.Content.ReadAsAsync<dynamic>().Result.d.ServerRelativeUrl;
using (HttpContent content = new MultipartFormDataContent())
{
content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)), "file", Path.GetFileName(filePath));
using (HttpResponseMessage uploadResponse = httpClient.PostAsync($"{siteUrl}/_api/web/GetFolderByServerRelativeUrl('{serverRelativeUrl}')/Files/add(url='{Path.GetFileName(filePath)}',overwrite=true)", content).Result)
{
uploadResponse.EnsureSuccessStatusCode();
}
}
}
}
}
static string GetAccessToken(string siteUrl, string username, string password)
{
string accessToken = null;
string resource = $"{siteUrl}/_api/contextinfo";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource);
request.Credentials = new NetworkCredential(username, password, "your-domain.sharepoint.com");
request.Method = "POST";
request.ContentLength = 0;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
string responseContent = streamReader.ReadToEnd();
accessToken = responseContent.Substring(responseContent.IndexOf("<d:FormDigestValue>") + 18);
accessToken = accessToken.Substring(0, accessToken.IndexOf("</d:FormDigestValue>"));
}
}
return accessToken;
}
}
在上述代码中,您需要将siteUrl
、username
、password
、filePath
和libraryName
变量替换为您的SharePoint网站URL、用户名、密码、要上传的文件路径和文档库名称。
希望这些信息能够帮助您解决问题。
领取专属 10元无门槛券
手把手带您无忧上云