You are viewing limited content. For full access, please sign in.

Question

Question

Downloading word document: Document getting corrupted

asked on March 1, 2023 Show version history

Hi,

I'm trying to programmatically download a word document from LF repository but It's getting corrupted / 'Word found unreadable content' message.

Here is my code snippet

public File GetFileInfo(int id) {
            DocumentInfo file = Document.GetDocumentInfo(id, this.Session);

            return new File() {
                Name = file.Name,
                Extension = file.Extension,
                Created = file.CreationTime,
                CreatedBy = file.Creator,
                Modified = file.LastModifiedTime,
                ModifiedBy = null, 
                Size = file.ElecDocumentSize
            };
        }

 

using (var Service = Import.PublishingService) {
                        var fileinfo = Service.GetFileInfo(identifier);

                        // upload file //
                        var totalsize = fileinfo.Size;
                        var offset = 0;

                        string tempfilename = Path.GetFileNameWithoutExtension(filename) + " " + Guid.NewGuid().ToString() + Path.GetExtension(filename);
                        tempfilelocation = System.Environment.GetEnvironmentVariable("eScribePath") + "\\Temp\\" + tempfilename;

                        using (FileStream fs = new FileStream(tempfilelocation, FileMode.Create, FileAccess.ReadWrite)) {
                            while (offset < totalsize) {

                                int count = 262144; 
                                byte[] buffer = Service.GetFileBytes(identifier, offset, count);

                                count = buffer.Length;

                                fs.Write(buffer, 0, count);

                                offset += count;
                            }
                        }

Any thoughts on why is this happening?

0 0

Replies

replied on March 1, 2023

It's hard to say without seeing the code for Service.GetFileBytes. The usual way for code like this to go wrong is that you lose track of how many bytes you read into a buffer. So maybe you only partially fill a byte array, but you write the whole thing out. One easy way to check - does the amount of data written match the amount of data read?

2 0
replied on March 2, 2023

Try this.

Code extracted from a workflow SDK activity.
replace RASession with Session

 

       

        private void Detache_fichier(int nID, string sPath, string sPrefix_Fich)
        {
            DocumentInfo doc = null;
            string mimeType = "";

            try
            {
                doc = (DocumentInfo)Entry.TryGetEntryInfo(nID, RASession);
                string sExtension = doc.Extension;
                string sOut = String.Format("{0}{1}{2}.{3}",sPath,doc.Name,sPrefix_Fich,sExtension);
                if(File.Exists(sOut))
                {
                    File.Delete(sOut);
                }
                using (FileStream fileStream = File.Create(sOut))
                {
                    using (LaserficheReadStream edocStream = doc.ReadEdoc(out mimeType))
                    {
                        edocStream.CopyTo(fileStream);
                    }
                }
            }
            catch
            {
                throw new Exception();
            }
        }

 

1 0
replied on March 2, 2023 Show version history

This method is preferred. Instead of reading ElecDocumentSize bytes (ElecDocumentSize is not guaranteed to be correct), read from the stream until there is no data left.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.