如何使用 CSOM 将(多个)文件上传到 SharePoint Online?

我有一个 Web 表单,可以将一些输入的数据作为一个新的列表项上传到 SharePoint 列表,效果很好。我试图添加一个新代码来通过两个单独的<asp:FileUpload>控件上传两个文件。以下代码protected void sendToSharePoint() {}无法将任何一个文件上传到指定的 SharePoint 文档库,更不用说两者了:


默认.aspx:


//Existing code


<asp:FileUpload ID="upldGradeReport" runat="server" />

<asp:FileUpload ID="upldExpenseReceipt" runat="server" />


<asp:Button ID="btnSubmitForm" OnClick="SubmitButton_Click" runat="server" Text="Submit" />

默认.aspx.cs:


using System;

using System.DirectoryServices;

using System.IO;

using System.Security;

using System.Web.UI;

using Microsoft.SharePoint.Client;

using ClientOM = Microsoft.SharePoint.Client;


//I left out the NameSpace and default public partial class wrapper, but they're here.


public ClientContext SPClientContext { get; set; }

public string SPErrorMsg { get; set; }


protected void SubmitButton_Click(object sender, EventArgs e) {

    sendToSharePoint();

    Response.BufferOutput = true;

    Response.Redirect("Submission.aspx");

}


protected void sendToSharePoint() {


    try {

        string siteUrl = "<sharepoint site url>";


        ClientContext clientContext = new ClientContext(siteUrl);

        clientContext.Credentials = new SharePointOnlineCredentials("<my username>", "<my password>");


        string sDocName = string.Empty;

        string sDocName1 = string.Empty;

        Uri uri = new Uri(siteUrl);

        string sSPSiteRelativeURL = uri.AbsolutePath;

        sDocName = UploadFile(upldGradeReport.FileContent, upldGradeReport.FileName, sSPSiteRelativeURL, "<sharepoint document library>");

        sDocName1 = UploadFile(upldExpenseReceipt.FileContent, upldExpenseReceipt.FileName, sSPSiteRelativeURL, "<sharepoint document library>");


        //prior CSOM code to insert values into a new List Item exists here


        clientContext.ExecuteQuery();

    } catch (Exception ex) {

        String ThisError = ex.Message;

    }

}

创建新 ListItem 并将表单的其余输入数据上传到单独的 SharePoint 列表的代码在提交时仍然有效,我已经确认凭据正确并且正在使用的帐户有权将文件上传到文档图书馆。


我究竟做错了什么?


繁华开满天机
浏览 121回答 1
1回答

森栏

我在本地环境中测试了以下代码;它工作正常。<div>&nbsp; &nbsp; <asp:FileUpload ID="upldGradeReport" runat="server" />&nbsp; &nbsp; <asp:FileUpload ID="upldExpenseReceipt" runat="server" />&nbsp; &nbsp; <asp:Button ID="btnSubmitForm" OnClick="SubmitButton_Click" runat="server" Text="Submit" /></div>protected void SubmitButton_Click(object sender, EventArgs e){&nbsp; &nbsp; sendToSharePoint();&nbsp; &nbsp; Response.BufferOutput = true;&nbsp; &nbsp; Response.Redirect("Submission.aspx");}protected void sendToSharePoint(){&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string siteUrl = "https://tenant.sharepoint.com/sites/lee";&nbsp; &nbsp; &nbsp; &nbsp; ClientContext clientContext = new ClientContext(siteUrl);&nbsp; &nbsp; &nbsp; &nbsp; SecureString securePassword = new SecureString();&nbsp; &nbsp; &nbsp; &nbsp; foreach (char c in "password".ToCharArray()) securePassword.AppendChar(c);&nbsp; &nbsp; &nbsp; &nbsp; clientContext.Credentials = new SharePointOnlineCredentials("lee@tenant.onmicrosoft.com", securePassword);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; string sDocName = string.Empty;&nbsp; &nbsp; &nbsp; &nbsp; string sDocName1 = string.Empty;&nbsp; &nbsp; &nbsp; &nbsp; Uri uri = new Uri(siteUrl);&nbsp; &nbsp; &nbsp; &nbsp; string sSPSiteRelativeURL = uri.AbsolutePath;&nbsp; &nbsp; &nbsp; &nbsp; sDocName = UploadFile(clientContext,upldGradeReport.FileContent, upldGradeReport.FileName, sSPSiteRelativeURL, "MyDoc");&nbsp; &nbsp; &nbsp; &nbsp; sDocName1 = UploadFile(clientContext,upldExpenseReceipt.FileContent, upldExpenseReceipt.FileName, sSPSiteRelativeURL, "MyDoc");&nbsp; &nbsp; &nbsp; &nbsp; //prior CSOM code to insert values into a new List Item exists here&nbsp; &nbsp; &nbsp; &nbsp; //clientContext.ExecuteQuery();&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String ThisError = ex.Message;&nbsp; &nbsp; }}public String UploadFile(ClientContext clientContext,Stream fs, string sFileName, string sSPSiteRelativeURL, string sLibraryName){&nbsp; &nbsp; string sDocName = string.Empty;&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var sFileURL = String.Format("{0}/{1}/{2}", sSPSiteRelativeURL, sLibraryName, sFileName);&nbsp; &nbsp; &nbsp; &nbsp; Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, sFileURL, fs, true);&nbsp; &nbsp; &nbsp; &nbsp; sDocName = sFileName;&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; sDocName = string.Empty;&nbsp; &nbsp; &nbsp; &nbsp; //SPErrorMsg = ex.Message;&nbsp; &nbsp; }&nbsp; &nbsp; return sDocName;}
打开App,查看更多内容
随时随地看视频慕课网APP