none
Uploading documents to library from FTP RRS feed

  • Question

  • Does anyone know of any code to upload documents from a FTP folder into Share Point document library? The idea is to replicate the folder structure present in the FTP inside a document library. 
    V
    Tuesday, August 16, 2011 2:59 PM

Answers

  • Hello,

    Here is code Snipplet

    protected void Button1_Click(object sender, EventArgs e)
            {
                string uploadedFilePath = @"define a path from where file will be uploaded";

                string sharePointListPath = "url/Shared%20Documents/";
       
        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();
        }

    Its Working Over Here Hope it will Help!!

     


    Thanks, Sudhanshu Shekhar(MCTS)
    • Marked as answer by Shimin Huang Friday, August 26, 2011 7:09 AM
    Wednesday, August 17, 2011 5:59 AM

All replies

  • One time upload or are you trying to synch the two continuously.


    Darrin Bishop Aptillon, www.aptillon.com
    Wednesday, August 17, 2011 5:03 AM
  • Hello,

    Here is code Snipplet

    protected void Button1_Click(object sender, EventArgs e)
            {
                string uploadedFilePath = @"define a path from where file will be uploaded";

                string sharePointListPath = "url/Shared%20Documents/";
       
        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();
        }

    Its Working Over Here Hope it will Help!!

     


    Thanks, Sudhanshu Shekhar(MCTS)
    • Marked as answer by Shimin Huang Friday, August 26, 2011 7:09 AM
    Wednesday, August 17, 2011 5:59 AM