none
Upload Excel file to Document Library read from a Web RRS feed

  • Question

  • Hi, I need to upload a  Excel File from an other web to a Document Library in my Share point, I use WebClient to read this file, but when I tried to save the Stream in the SPFolder I recived the error:

    The stram does not support seek operations.

    My code:

     string file_url = url +
    "/files/file.xlsx";
    Stream file_web = client.OpenRead(file_url);
    byte[] result = null;
    int count_lenght = Convert.ToInt32(file_web.Length);
    using (BinaryReader reader = new BinaryReader(file_web))
    {
    result = reader.ReadBytes(count_lenght);
    }
    SPSite siteCollection = new SPSite(SPContext.Current.Site.Url);
    SPWeb Site = siteCollection.OpenWeb();
    Site.AllowUnsafeUpdates = true;
    SPFolder ctcFolder = Site.GetFolder(ConfigurationManager.AppSettings["Destination"].ToString());
    SPFile file = ctcFolder.Files.Add("NewFile.xlsx", result, true); //The error occurs here
    Guid code = file.UniqueId;

    Thanks
    Saturday, August 29, 2009 10:19 PM

Answers

  • When using either the webClient or webRequest objects the streams that are returned do not support seeking, meaning that you cannot determine their length at runtime. This is because it is using native http calls and the content length is not known. So you must read the stream in chunks or pieces until no more bytes can be read. Then the byte array must be copied to another byte array making the code very messy. You can use the copy.asmx web service GetItem method to return the document in one call and one byte array. You can then use this byte array to add to the files collection.

    copyservice.Copy c = new copyservice.Copy();
    c.Url = "http://basesmcdev2/sites/tester1/_vti_bin/copy.asmx";
    c.UseDefaultCredentials = true;
    
    byte[] myBinary = new byte[] { };
    copyservice.FieldInformation information = new copyservice.FieldInformation();
    copyservice.FieldInformation[] info = { information };
    
    uint result = c.GetItem(url, out info, out myBinary);

    certdev.com
    • Marked as answer by Chengyi Wu Friday, September 11, 2009 3:01 AM
    Sunday, August 30, 2009 3:34 AM

All replies

  • When using either the webClient or webRequest objects the streams that are returned do not support seeking, meaning that you cannot determine their length at runtime. This is because it is using native http calls and the content length is not known. So you must read the stream in chunks or pieces until no more bytes can be read. Then the byte array must be copied to another byte array making the code very messy. You can use the copy.asmx web service GetItem method to return the document in one call and one byte array. You can then use this byte array to add to the files collection.

    copyservice.Copy c = new copyservice.Copy();
    c.Url = "http://basesmcdev2/sites/tester1/_vti_bin/copy.asmx";
    c.UseDefaultCredentials = true;
    
    byte[] myBinary = new byte[] { };
    copyservice.FieldInformation information = new copyservice.FieldInformation();
    copyservice.FieldInformation[] info = { information };
    
    uint result = c.GetItem(url, out info, out myBinary);

    certdev.com
    • Marked as answer by Chengyi Wu Friday, September 11, 2009 3:01 AM
    Sunday, August 30, 2009 3:34 AM
  • Hi Steve,
                    I am trying to use your approach i.e. copy webservice:getItem method.
    But my byte array is returned as a null. Any thoughts? I have checked the authentication/permissions bit with both windows and forms based.
    I don't quite understand as what else I am missing.

    Thanks,
    Nick
    Monday, December 21, 2009 11:40 PM