I am trying to upload XML files from a VM server to a Sharepoint url(I am trying to upload under shared documents in a library )
Note: I have access to the VM server, I have access to upload files manually on the SharePoint site.
but I need this to be automated, so that as soon as a new XML file arrives on the VM server, the file should get uploaded on the SharePoint
I have developed a web application page using visual studio 2010
Followed the step below
1.Creating an ASP.NET Web application in Microsoft Visual Studio 2010
2.Importing the System.IO namespace and the System.Net namespace
3.Creating the file upload form
4.Adding the upload code to the solution
I diligently followed the MSDN link below:
http://msdn.microsoft.com/en-us/library/dd902097(v=office.12).aspx[
^]
However whenever I try to upload a file[tried uploading XML, doc, pdf(my requirment to upload XML)]
I get the following error
"Object reference not set to an instance of an object"
Questions:
1.Please review the code and I appreciate any help for resolving the error.
2.After resolving this error, could any one please tell me what security measures I need to take for secure file upload.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Fileupload.aspx.cs" Inherits="shera.Fileupload" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
public void Button1_Click(object sender, EventArgs e)
{
string uploadedFilePath = @"C:\\Users\\Documents\\project files\\XML samples";
string sharePointListPath =" Sharepoint url(I am trying to upload under shared documents in a library )"
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.";
}
}
public void UploadFileToSharePoint(string UploadedFilePath,
string SharePointPath)
{
WebResponse response = null;
try
{
WebRequest request = WebRequest.Create(SharePointPath);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "PUT";
byte[] buffer = new byte[1024];
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);
}
}
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 />
<br />
<asp:Button ID="UploadButton" runat="server" OnClick="Button1_Click"
Text="Upload File" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>