Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

C# asp.net web application using Visual Studio 2008 converted from 1.1 framework.

I am using file upload control in aspx file to upload a single file to the server on button click. This also contains two methods 1) to upload the file to SharePoint 2007 library and 2) to update sql database with path and filename of uploaded file.

The following code runs successfully whilst debugging within Visual Studio on local development machine or if I remote into the dev server machine where I have it deployed. The upload to SharePoint fails at the UploadFileToSharePoint() method when I run the web app from client machine browser, yet the upload to local and update to db does succeed.

I do not understand why it works locally but only partially work after it's deployed. Am I missing something in the server deployement setup? I am using Windows Authentication.

Error Details:

Message: Object reference not set to an instance of an object., 
Source: App_Web_jybzycof, 
TargetSite: Void UploadFileToSharePoint(System.String, System.String), 
StackTrace: at ASP.fileupload_aspx.UploadFileToSharePoint(String UploadedFilePath, String SharePointPath) in c:\inetpub\wwwroot\webappTest\FileUpload.aspx:line 123 at ASP.fileupload_aspx.btnUploadFile_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webappTest\FileUpload.aspx:line 64,
uploadedFilePath: C:\folder1\folder2\folder3\uploads\, 
sharePointListPath: http://server/site/subsite/library


protected void btnUploadFile_Click(object sender, EventArgs e)
    {   
        string strFilenameToUpload = string.Empty;

        //check if file was selected to upload
        if (FileUpload1.HasFile)
            try
            {
               //get file name
               strFilenameToUpload = FileUpload1.FileName;  

//Save selected file into specified location 
                FileUpload1.SaveAs(uploadedFilePath + FileUpload1.FileName);                


                Label1.Text = "<br />Success! <br /> <br />" +
                    "File name: [" + FileUpload1.FileName + "] " + "was uploaded to  <a href='http://server/site/subsite/library/'>SharePoint.</a>" + 
                     "<br />" +
                     "File size: " + FileUpload1.PostedFile.ContentLength + " bytes<br />" + 
                     "Content type: " + FileUpload1.PostedFile.ContentType; 


                //establish url for this upload
                strDocumentUrlToUpload = sharePointListPath + "/" + FileUpload1.FileName;      

                UploadFileToSharePoint(
                    uploadedFilePath + FileUpload1.FileName,
                    sharePointListPath + FileUpload1.FileName);

            }
            catch (Exception ex)
            {
                Label1.Text = "ERROR Message: " + ex.Message.ToString() + 
", Source:  " + ex.Source + 
", TargetSite: " + ex.TargetSite + ", StackTrace: " + ex.StackTrace +
", uploadedFilePath: " + uploadedFilePath + 
               ", sharePointListPath: " + sharePointListPath; 

            }
        else
        {
            Label1.Text = "You have not specified a file.";
        }

        //Save SharePoint path, filename to database
        SqlConnLib updater = new SqlConnLib();
        updater.UpdateFileinfo(strDocumentUrlToUpload, strFilenameToUpload);  
    }

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

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

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

            // Allocate a 1K buffer to transfer the file contents.
            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>

UPDATE: Since this didn't prompt any responses - I'll share what I learned from my research. I learned that the solution is to write a custom Web Service to upload from a remote server to SharePoint 2007. There is an OOB SharePoint Web Service but it will only copy from one SharePoint installation to another-it will not work from remote (non SP installation) server to SP. The Client Object Model is one technique that will work but it does not work with SP 2007.(It will supposedly work with SP 2010 & I assume 2013). There is plenty of solutions/examples to find by Googling but most of them do not tell you they won't work remotely - they only work when the app is run from the same server where it is installed.

For now, I will upload to server as a solution. Then modify the app after we upgrade our SharePoint installation.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.