1

I have developed the file uploading into document library in SharePoint 2013 using Managed client object model. Here is my 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>

and here is my 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;

        }
    }
}

It is working fine when I am uploading small files, it through an error when I'm uploading large file.

Error:" Maximum request length exceeded "

Please, help me how to resolve issue.

1 Answer 1

1

Try with increasing 'maxRequestLength' in web.config file of your web application.

<httpRuntime maxRequestLength="1048576"/>

path of web.config is --> C:\inetpub\wwwroot\wss\VirtualDirectories\Your web application folder

Note: please take backup of web.config before making changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.