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

Question

Question

ImportEdoc(mime, stream)

SDK
asked on February 3, 2016 Show version history

Greetings Laserfiche Community,

 

I am having some trouble importing electronic documents with the DocumentImporter.ImportEdoc() method and a data stream.  SDK documentation states you can import an electronic document by using the method above and passing it a mimetype and a stream.

ImportEdoc(String, Stream)

Imports data from a stream into the current Laserfiche document as an electronic document, replacing any existing electronic document

 

This is some sample test code that I am using to try and figure out what I am doing wrong, but I am not sure I am missing.  Any help would be greatly appreciated.  Thank you!

 

            using (FileStream file = File.OpenRead("G:\\Laserfiche Import Agent 9 Quick Start.pdf"))
            {
                
                //var ms = new MemoryStream();
                //file.CopyTo(ms);
                DocumentImporter DI = new DocumentImporter();

                DI.Document = docInfo;  //docInfo was initialized earlier in code that is not attached
                DI.ImportEdoc("application/pdf", file);

            }

 

0 0

Replies

replied on February 3, 2016

Is it returning an error? I don't see an obvious problem with the code.

1 0
replied on February 3, 2016

No error is returned.  Just nothing is put into the document.  Wish there was an error message, it would definitely help to debug this.

0 0
replied on February 3, 2016

Are you sure nothing is being written? Check the file size in the document properties (screenshot below). The entry icon won't change with this code because it isn't changing the extension.

0 0
replied on February 3, 2016

Here are the properties on the document.

0 0
replied on February 3, 2016

Are you catching exceptions thrown by that code? I can't think of anything else that could be going wrong, i tried the code myself and it writes the edoc correctly.

0 0
replied on February 3, 2016

I tried adding the a try and catch below but the catch is never hit.  When I step through the code, I can see the mimetype being set for the docInfo object, but not for the electronic document content itself.

 

 

                try
                {
                    DI.Document = docInfo;
                    DI.ImportEdoc("application/pdf", file);
                }
                catch (System.Exception exp)
                {
                    File.WriteAllText(@"G:\anyerror?.txt", "Error please: " + exp.Message);
                }

0 0
replied on February 3, 2016

Also, if it helps, when I specify the file directly where I specify the stream, then it works fine.

0 0
replied on February 4, 2016

Your exception handler specifies an illegal file name - Windows doesn't allow question marks.

0 1
replied on February 14, 2016 Show version history

Hi Brian -

If the code never hits the exception handle, the question mark would not make a difference. 

I am having what may be a similar issue.  Using       

       LF_DocImport.Document = LF_DocInfo and

       LF_DocImport.ImportEdoc(_MimeType, DocData)

for a pdf file stored in a memory stream, I can add the pdf pages to the document (created via             LF_DocInfo.Create(LF_FolderInfo, sDocumentName, sVolume, EntryNameOption.AutoRename))

but the document does not open as a pdf - instead it asks me what application I want to use.  This is true even when I help the Doc Info object in advance by setting the mime type to application/pdf.  It is also true regardless of the pdf view settings I use.

When dragging the same file into the client, or importing it as a file from disk, there is no problem. 

Looking at TOC, the entries are identical except the file imported from a memory stream has no extension.  edoc_ext is NULL.  Note the two rows below. The first document was imported via drag and drop, and the second was imported as a stream.  The second entry asks for an application (And will launch only after you select Adobe) and the first one displays in the LF client without issue. So the question is, what's the trick to getting this to work properly?

toc.png
toc.png (22.67 KB)
0 0
replied on February 15, 2016

If you are importing via a MemoryStream, you need to set the Extension property on the DocumentInfo manually:

LF_DocInfo.Extension = "pdf";
LF_DocInfo.Save();

 

2 0
replied on February 15, 2016

If the code never hits the exception handle, the question mark would not make a difference.

 

That is true, and I didn't mean to suggest that it was that cause of your problems.  Having errors in your exception handling code can be especially confounding because it can lead you to make incorrect conclusions about what is and isn't working in the rest of your program, e.g. that since no file called "anyerror?.txt" was created, then all of the code in the try block must have completed successfully.

1 0
replied on February 15, 2016 Show version history

Thanks, Robert -

This is what I had missed:  LF_DocInfo.Save();  I also found that you had to provide and extension for a .docx document and I will assume any other type of electronic file. 

Since this is a memory stream, we are now we are back to the problem of resolving mime types to file extensions. This starts with the problem of detecting the mime type in the first place, and is one of those things that ends up being way more difficult than it should be.

You can use the methods of urlmon.dll (google, or  http://stackoverflow.com/questions/22947953/vb-net-equivalent-to-c-sharp-for-this-method-for-getting-mime-types ) but this only handles 26 mime types, and is not accurate for any Office documents. 

You can read the beginning of the file and map the bytes (See: http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature/13614746#13614746

scroll down for the response that starts "Edit: Just use Mime Detective")

Or you can use Mime Detective, which looks effective,

Or you can do what we did and find a way to get the file extension and look the mime type up from the registry, or via a large table.  Bert posted the large table method somewhere and Ed posted some nice registry code here:
http://answers.laserfiche.com/questions/47718/Empower-2014--EDM351--Advanced-Applied-SDK--What-would-you-like-to-see
 

Having spent way too much time on this issue, hopefully this will save someone some steps.

****************************************************************************

Update:  I dug into Mime Detective and while tricky to implement, it provides the best results.

Location: https://github.com/Muraad/Mime-Detective

Add a reference to the dll, and then, Imports MimeDetective

Add this line to MimeTypes.cs, The graphics region:

public readonly static FileType TIF = new FileType(new byte?[] { 73, 73, 42, 0 }, "tif", "image/tif");

Add TIF to the constructor:

            types = new List<FileType> {PDF, WORD, EXCEL, JPEG, TIF, ZIP,

They referenced tif files but forgot to add the info.

Usage:

            Dim _MimeFileType As MimeDetective.FileType
            _MimeFileType = MimeDetective.MimeTypes.GetFileType(bytDocData)
            _MimeType = _MimeFileType.Mime()

            _Extension = _MimeFileType.Extension

(_MimeType and _Extension are strings, bytDocData is a byte array.)

It's the only method I found that works from a memory stream and is smart enough to differentiate a docx from a zip file.

However, it writes the file to disk as a temp file, so I am using urlmon.dll first. This operates in memory, and if it does not return an image format, then we'll call MimeDetective.

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

Sign in to reply to this post.