在sharepoint 2013中使用托管客户端对象模型上传文件
本文关键字:对象模型 客户端 文件 2013 sharepoint | 更新日期: 2023-09-27 18:06:56
我在sharepoint 2013中使用托管客户端对象模型开发了文件上传到文档库的功能。这是我的upload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2" align="center">
<asp:Label Font-Bold="true" Text="Upload a documents to SharePoint 2013" runat="server" ID="lblSharePoint" />
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="lblLocalPath" runat="server" Text="Local Path:"></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileLocalPath" Width="350" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblSharePointURL" runat="server" Text="SharePoint Site URL:"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtSharePointURL" Width="350" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblLibraryName" runat="server" Text="Library Name:"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtLibraryName" Width="350" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
这里是upload。aspx.cs
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public ClientContext SPClientContext { get; set; }
public Web SPWeb { get; set; }
public string SPErrorMsg { get; set; }
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
string sURL = txtSharePointURL.Text;
string sDocName = string.Empty;
if (!string.IsNullOrWhiteSpace(sURL) && !string.IsNullOrWhiteSpace(txtLibraryName.Text) && (FileLocalPath.HasFile))
{
bool bbConnected = Connect(sURL, "abhishekr", "A123bhishek", "saras");
if (bbConnected)
{
Uri uri = new Uri(sURL);
string sSPSiteRelativeURL = uri.AbsolutePath;
sDocName = UploadFile(FileLocalPath.FileContent, FileLocalPath.FileName, sSPSiteRelativeURL, txtLibraryName.Text);
if (!string.IsNullOrWhiteSpace(sDocName))
{
lblMsg.Text = "The document " + sDocName + " has been uploaded successfully..";
lblMsg.ForeColor = Color.Blue;
}
else
{
lblMsg.Text = SPErrorMsg;
lblMsg.ForeColor = Color.Red;
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
public bool Connect(string SPURL, string SPUserName, string SPPassWord, string SPDomainName)
{
bool bConnected = false;
try
{
SPClientContext = new ClientContext(SPURL);
SPClientContext.Credentials = new NetworkCredential(SPUserName, SPPassWord, SPDomainName);
SPWeb = SPClientContext.Web;
SPClientContext.Load(SPWeb);
SPClientContext.ExecuteQuery();
bConnected = true;
}
catch (Exception ex)
{
bConnected = false;
SPErrorMsg = ex.Message;
Response.Write(ex.Message.ToString());
}
return bConnected;
}
public string UploadFile(Stream fs, string sFileName, string sSPSiteRelativeURL, string sLibraryName)
{
string sDocName = string.Empty;
try
{
if (SPWeb != null)
{
var sFileUrl = String.Format("{0}/{1}/{2}", sSPSiteRelativeURL, sLibraryName, sFileName);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(SPClientContext, sFileUrl, fs, true);
sDocName = sFileName;
}
}
catch (Exception ex)
{
sDocName = string.Empty;
SPErrorMsg = ex.Message;
Response.Write(ex.Message.ToString());
}
return sDocName;
}
}
}
当我上传小文件时它工作正常,当我上传大文件时它会出现错误。
错误:" Maximum request length exceeded "
请告诉我如何解决这个问题
你可以在SharePoint中这样定义最大请求长度:https://blogs.technet.microsoft.com/sammykailini/2013/11/06/how-to-increase-the-maximum-upload-size-in-sharepoint-2013/
在中央管理>应用程序管理>管理Web应用程序>选择所需的Web应用程序并单击功能区上的常规设置
web。像这样编辑httpRuntime节点(对于2GB文件和无限执行):