We help IT Professionals succeed at work.

Upload a file to SharePoint 2007 with C#.NET

scross1276
scross1276 used Ask the Experts™
on
I grabbed the code below from a MSDN site (http://msdn.microsoft.com/en-us/library/dd902097(office.12).aspx).  The purpose of it is to upload a file from my local machine to our corporate SharePoint 2007 site.  All of the uploads fail saying "ERROR: Access to the path 'C:\Test file.txt' is denied."

Does anyone know why I'm getting that error?
<asp:FileUpload ID="fileUpload" runat="server" Width="350px" />
                    <br />
                    <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload File" />
                    <br />
                    <asp:Label ID="lblFileUpload" runat="server"></asp:Label>









protected void btnUpload_Click(object sender, EventArgs e)
        {
            string sharePointServer = ConfigurationManager.AppSettings["SharePointServer"].ToString();
            string uploadedFilePath = @"C:\\";
            string sharePointListPath = "http://" + sharePointServer + "InfoWorld/IT/ITTech/eForm%20Document%20Attachments";

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

                    lblFileUpload.Text = "File name: " +
                         fileUpload.PostedFile.FileName + "<br />" +
                         fileUpload.PostedFile.ContentLength + " bytes<br />" +
                         "Content type: " +
                         fileUpload.PostedFile.ContentType;

                    UploadFileToSharePoint(
                        uploadedFilePath + fileUpload.FileName,
                        sharePointListPath + fileUpload.FileName);
                }
                catch (Exception ex)
                {
                    lblFileUpload.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                lblFileUpload.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[4096];// 4 mb file limit.

                // 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();
            }
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try granting your service account running the web site/page access to the C:\ drive.

I assume you are running under Network Services, which doesn't have read access to your local c:\ drive by default.

Author

Commented:
Are you talking about the IIS Admin Service?  If so, it's already running as Local System.
Commented:
Here is a much more detail explanation and solution - http://www.codeproject.com/KB/aspnet/Ahmed_Kader.aspx
Hi scross1276, Could you add the users, Central administrator account and share some folder in the server and give that as the path.
dont forget to add the users list in security also

Author

Commented:
The problem is on the client side though, each local machine.  Adding everyone to a new share on the server side won't fix that.

Author

Commented:
Neither solution actually solved my problem, but I'll split the points for helping out.