我的想法是,我有一个表单,我希望允许用户将文件上传到我的SharePoint文档库。我不只是在“HasFile'之后做什么”。声明。我的代码的一些小片段如下。我只是不确定如何从fileupload ID进入我的文档库。请提供代码。
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
服务器端代码
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//????
}
}
答案 0 :(得分:0)
可以使用以下方法将文档上载到SharePoint库:
/// <summary>
/// Uploads the specified file into a SharePoint Library
/// </summary>
/// <param name="url">site url</param>
/// <param name="creds">Credentials</param>
/// <param name="listTitle">List Title</param>
/// <param name="fileName">File Name</param>
/// <param name="stream">File Stream</param>
private static void UploadFile(string url,string listTitle, string fileName, Stream stream)
{
using (var clientContext = new ClientContext(url))
{
var list = clientContext.Web.Lists.GetByTitle(listTitle);
clientContext.Load(list.RootFolder);
clientContext.ExecuteQuery();
var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fileName);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, stream, true);
}
}
注意:如果您需要传递Credentials,请使用 ClientRuntimeContext.Credentials property设置 显示的客户端上下文的身份验证信息 下面:
clientContext.Credentials = credentials;
用法
if (FileUpload1.HasFile)
{
UploadFile(webUrl, listTitle, FileUpload1.FileName, FileUpload1.PostedFile.InputStream);
}