使用sharepoint 2013中的托管客户端对象模型上载文件

时间:2016-09-13 06:10:37

标签: c# sharepoint-2013 csom

我已使用托管客户端对象模型将文件上传到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;

        }
    }
}

当我上传小文件时它正常工作,当我上传大文件时它会出错。

错误:“超出最大请求长度”

请帮我解决问题。

1 个答案:

答案 0 :(得分:0)

您可以在SharePoint中定义最大请求长度,如下所示: https://blogs.technet.microsoft.com/sammykailini/2013/11/06/how-to-increase-the-maximum-upload-size-in-sharepoint-2013/

在中央管理员&gt;应用程序管理&gt;管理Web应用程序&gt;选择所需的Web应用程序,然后单击功能区上的“常规设置”

在web.config中编辑像这样的httpRuntime节点(对于2GB文件和无限制执行):