none
Upload to document library from a stream c#? RRS feed

  • Question

  • This code uploads the file, but says its corrupted when openning in word, what am I doing wrong?

    //Write Doc to stream
           Stream docStream = mainDocPart.GetStream();
           MemoryStream mStream = new MemoryStream();
           StreamWriter sw = new StreamWriter(mStream);
           sw.Write(docStream);
    
           sw.Flush();
           byte[] contents = new byte[mStream.Length];
           mStream.Read(contents, 0, (int)mStream.Length);
    
           web.Files.Add("http://siteurl/doclib/file.docx",mStream, true);
    

     

     

     

    Thursday, October 28, 2010 9:48 PM

Answers

  • I figured it out...

    not supposed to call document.MainDocumentPart.Document.Save(memStream) with the memory stream param, just document.MainDocumentPart.Document.Save();

    and I didnt dispose of the document before writing it to SP, d'oh....

    Anyways thanks for the pointers everyone...

    • Marked as answer by Porter Wang Wednesday, November 10, 2010 6:02 AM
    Monday, November 8, 2010 7:21 PM

All replies

  • try this,
    using System.Net;

    WebClient wc =new WebClient();
    wc.UseDefaultCredentials = true;
    byte[] response = wc.UploadFile(string.Concat(Sitename, "/Shared Documents/", Filename), "PUT", System.Windows.Forms.Application.StartupPath + @"\" + Filename);

     


    André Lage Microsoft SharePoint, CRM and Sybase Consultant
    Blog:http://aaclage.blogspot.com
    Codeplex:http://spupload.codeplex.com/http://simplecamlsearch.codeplex.com/

    Thursday, October 28, 2010 10:21 PM
  • If you want to upload a file, André's solution should work fine.  You did not say where your stream is coming from. What is "mainDocPart"? Are you sure the source represents a valid Word document?

    Here's an example that starts with a file stream:

    FileStream stream = File.OpenRead("C:\\email note.docx");
    byte[] contents = new byte[stream.Length];
    stream.Read(contents, 0, (int)stream.Length); stream.Close();
    
    SPFolder folder = web.Folders["Shared Documents"];
    folder.Files.Add("email note.txt", contents);
    folder.Update();
    

     

     


    Mike Smith TechTrainingNotes.blogspot.com
    Friday, October 29, 2010 12:18 AM
  • SPFolder sourceFolder = null;
    SPFile sourceFile = null;
    SPFolder destinationFolder = null;
    SPFile destinationFile = null;

    //--Fill the values in sourceFile object

    byte[] filebytes = sourceFile.OpenBinary();//Open binary of the file

     destinationFile = destinationFolder.Files.Add(destinationFileURL, filebytes,sourceFile.Properties, true);

    • Edited by Mike Walsh FIN Wednesday, November 3, 2010 10:54 AM sig removed - don't ask people to mark your answers
    Friday, October 29, 2010 7:00 AM
  • using (SPSite _site = new SPSite(SiteURL))
    {
     byte[] content = contentInByteArray;

     using (SPWeb _web = _site.OpenWeb())
     {
      SPFolder _folder = _web.Folders[_web.Url + "/" + "DocumentLibrary_name"];
      SPFile _file = _folder.Files.Add("filename.ext", content);
     }
    }

    I am using like this is working fine!


    Suman Pradhan
    Friday, October 29, 2010 1:02 PM
  • Thanks a bunch everyone, sorry I should have been more specific, please let me elaborate...

    I am creating the word doc on the fly using OpenXML, and then trying to save it to a doc library... Im doing it a little differently than in my first post, but I still get the same result...

    Something like this...

    using (MemoryStream memStream = new MemoryStream())
        {
         using (WordprocessingDocument document = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document))
         {
    
          //Create Word Document
    
          MainDocumentPart mainDocPart = document.AddMainDocumentPart();
          mainDocPart.Document = new Document();
    Body body = new Body();
    
    //Save the Doc
          mainDocPart.Document.Append(body);
    document.MainDocumentPart.Document.Save();
    
    AddToSharePoint(memStream, docLibTitle, web.Site.Url, filename);
    
    }
    }
    
    
      public static void AddToSharePoint(MemoryStream memoryStream, string docLibTitle, string siteUrl, string fileName)
      {
    
       using (SPSite spSite = new SPSite(siteUrl))
       {
        //Get the document library object
        SPList docLib = spSite.RootWeb.Lists[docLibTitle];
    
        SPFile file = docLib.RootFolder.Files.Add(fileName, memoryStream, true);
    
        file.Update();
    
       }
      }
    
    Any ideas? Thanks again...
    • Edited by Sean Sharepoint Tuesday, November 2, 2010 8:46 AM more polite :)
    Monday, November 1, 2010 9:54 PM
  • Thursday, November 4, 2010 3:03 PM
  • I am using MOSS 2007.

     

    Thursday, November 4, 2010 3:26 PM
  • Yea... looking further into it. The Microsoft.SharePoint.Client namespace isnt available for MOSS 2007.

    Thanks

    Thursday, November 4, 2010 7:01 PM
  • yes yes dont exist with moss 2007 client object model  is a new feature with 2010.
    I was thinking if you can use something there for moss 2007.
    André Lage Microsoft SharePoint, CRM and Sybase Consultant
    Blog:http://aaclage.blogspot.com
    Codeplex:http://spupload.codeplex.com/http://simplecamlsearch.codeplex.com/
    Thursday, November 4, 2010 7:22 PM
  • The problem isnt uploading the file, the file uploads and I can click on it, and Word tries to open the document, but then it says the "Contents are Corrupt", and the file doesnt open.
    Thursday, November 4, 2010 8:24 PM
  • yes i know i understand,

    One question, is a docx file? can you rename as zip and open to verify if have broke xml file line or something...


    André Lage Microsoft SharePoint, CRM and Sybase Consultant
    Blog:http://aaclage.blogspot.com
    Codeplex:http://spupload.codeplex.com/http://simplecamlsearch.codeplex.com/
    Thursday, November 4, 2010 8:37 PM
  • It is, and I have noticed that it doesnt hold all of the .xml files, only the styles.xml file, which I get a reference to and save. So this is the problem, but I thought I was saving everything correctly.
    using (MemoryStream memStream = new MemoryStream())
           {
             using (WordprocessingDocument document = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document))
             {
               //Create Word Document
               MainDocumentPart mainDocPart = document.AddMainDocumentPart();
               mainDocPart.Document = new Document();
    
               //Add styles.xml 
               StyleDefinitionsPart styleDefinitionsPart = mainDocPart.AddNewPart<StyleDefinitionsPart>();
    
               FileStream stylesTemplate = new FileStream(templatePath, FileMode.Open, FileAccess.Read);
               styleDefinitionsPart.FeedData(stylesTemplate);
               styleDefinitionsPart.Styles.Save();
    
    Body body = new Body();
    
    Paragraph pPos = new Paragraph(new ParagraphProperties(new ParagraphStyleId { Val = "ResumePosition" }));
                   Run rPos = new Run();
                   Text tPos = new Text("Test");
                   rPos.Append(tPos);
                   pPos.Append(rPos);
    
    body.Append(pPos);
    
    mainDocPart.Document.Append(body);
               document.MainDocumentPart.Document.Save(memStream);
    
    web.Files.Add(web.Url + @"/" + docLibTitle + @"/" + emp.DisplayName + ".docx", memStream, true);
    
               memStream.Close();
    }
    }
    Here's a better example of how I am compiling the document and saving to Sharepoint. Since I included the memStream variable in the .Save(memStream); method I now get Error:

    Cannot perform a read/write operation in write-only or read-only modes.

    Thanks for your help.

    Thursday, November 4, 2010 9:47 PM
  • I figured it out...

    not supposed to call document.MainDocumentPart.Document.Save(memStream) with the memory stream param, just document.MainDocumentPart.Document.Save();

    and I didnt dispose of the document before writing it to SP, d'oh....

    Anyways thanks for the pointers everyone...

    • Marked as answer by Porter Wang Wednesday, November 10, 2010 6:02 AM
    Monday, November 8, 2010 7:21 PM
  • Hi Sean,

    Please share the code.It would be helpful for me.

    Thanks in Advance.


    with regards Ravichandra

    Friday, July 21, 2017 9:17 AM