首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何将文件上传到sharepoint中的文档库?

要将文件上传到SharePoint中的文档库,您可以按照以下步骤操作:

  1. 登录到SharePoint网站。
  2. 导航到您要上传文件的文档库。
  3. 在文档库中,单击“上传”按钮。
  4. 浏览您的计算机,找到要上传的文件。
  5. 选择文件后,单击“确定”按钮。
  6. 文件将上传到文档库中。

如果您需要上传多个文件,可以使用“上传多个文件”功能。只需要在步骤3中选择“上传多个文件”选项,然后按照提示操作即可。

如果您需要将文件上传到SharePoint中的特定文件夹,可以先导航到该文件夹,然后按照上述步骤上传文件。

如果您需要使用编程方式上传文件到SharePoint中的文档库,可以使用SharePoint的CSOM(客户端对象模型)或REST API。以下是使用CSOM上传文件的示例代码:

代码语言:csharp
复制
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();
    }
}

在上述代码中,您需要将siteUrlusernamepasswordfilePathlibraryName变量替换为您的SharePoint网站URL、用户名、密码、要上传的文件路径和文档库名称。

如果您需要使用REST API上传文件,可以使用以下代码:

代码语言:csharp
复制
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;
    }
}

在上述代码中,您需要将siteUrlusernamepasswordfilePathlibraryName变量替换为您的SharePoint网站URL、用户名、密码、要上传的文件路径和文档库名称。

希望这些信息能够帮助您解决问题。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券