1

I am using below code for Uploading File to SharePoint from ASP.NET Web Applications. But while clicking on Upload Button i am getting this error "ERROR: Object reference not set to an instance of an object." Please help me by resolving the issue.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>


    <script runat="server">

        protected void Button1_Click(object sender, EventArgs e)
        {
            string uploadedFilePath = @"C:\Temp\Uploads\";
            string sharePointListPath =
                "http://<Server>/<ListName>/";

            if (FileUpload1.HasFile)
                try
                {
                    FileUpload1.SaveAs(
                        uploadedFilePath + FileUpload1.FileName);

                    Label1.Text = "File name: " +
                         FileUpload1.PostedFile.FileName + "<br>" +
                         FileUpload1.PostedFile.ContentLength + " bytes<br>" +
                         "Content type: " +
                         FileUpload1.PostedFile.ContentType;

                    UploadFileToSharePoint(
                        uploadedFilePath + FileUpload1.FileName,
                        sharePointListPath + FileUpload1.FileName);
                }
                catch (Exception ex)
                {
                    Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                Label1.Text = "You have not specified a file.";
            }
        }

        protected void UploadFileToSharePoint(string UploadedFilePath,
            string SharePointPath)
        {
            WebResponse response = null;

           try
            {

                // Create a PUT Web request to upload the file.
                WebRequest request =  WebRequest.Create(SharePointPath);

                request.Credentials = CredentialCache.DefaultCredentials;
                request.Method = "PUT";

                // Allocate a 1 KB buffer to transfer the file contents.
                // You can adjust the buffer size as needed, depending on
                // the number and size of files being uploaded.
                byte[] buffer = new byte[1024];

                // Write the contents of the local file to the
                // request stream.
                using (Stream stream = request.GetRequestStream())
                using (FileStream fsWorkbook = File.Open(UploadedFilePath,
                    FileMode.Open, FileAccess.Read))
                {
                    int i = fsWorkbook.Read(buffer, 0, buffer.Length);

                    while (i > 0)
                    {
                        stream.Write(buffer, 0, i);
                        i = fsWorkbook.Read(buffer, 0, buffer.Length);
                    }
                }

                // Make the PUT request.
                response = request.GetResponse();
            }
            catch (Exception ex)
            {
                throw ex;
           }
            finally
            {
                response.Close();
            }
        }

</script>


</head>
<body>
    <form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" Width = "500px" />
<br />
<br />
<asp:Button ID="UploadButton" runat="server" OnClick="Button1_Click"
    Text="Upload File" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
  • Please post the error stack trace also... – Aakash Maurya Aug 18 '16 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.