csabo

Adding documents from database

0 votes
I already have created the package and now want to add documents to it. I have file data in a byte array from the database. How do I add a document to the package with the byte array? Thanks Colleen

Approved Answer

Reply to: Adding documents from database

0 votes
Hey Colleen, The easiest way to do so would be to convert the byte array to a stream using the MemoryStream class. Here's an example on how to upload a document from a byte array:
        PackageId packageId = new PackageId("8e03d324-39bb-40fc-9279-ed20f5949a7b");
        DocumentPackage package = eslClient.GetPackage(packageId);

        byte[] doc = File.ReadAllBytes(@"PDF_FILE_PATH");
        Stream stream = new MemoryStream(doc);

        Document document = DocumentBuilder.NewDocumentNamed( "First Document" )
                                .FromStream( stream, DocumentType.PDF )
                                .WithSignature( SignatureBuilder.SignatureFor( "[email protected]" )
                                .OnPage( 0 )
                                .WithField( FieldBuilder.CheckBox()
                                .OnPage( 0 )
                                .AtPosition( 400, 200 )
                                .WithValue( FieldBuilder.CHECKBOX_CHECKED ) )
                                .AtPosition( 100, 100 ) )
                    .Build();

            eslClient.UploadDocument(document, package);
Haris Haidary OneSpan Technical Consultant

Reply to: Adding documents from database

0 votes
Haris, I'm getting an error message on the eslClient.UploadDocument line of code. The fileModel.data is the byte array. I see in the database that the content type is application/pdf for the TestingApprovals.pdf file. I'm able to manually add this pdf file through the website, so it's fine there. Thoughts? Here is my code:
 EslClient eslClient = getEslClient(companyId);
                PackageId pkgId = new PackageId(packageId);
                DocumentPackage pkg = eslClient.GetPackage(pkgId);

                Stream stream = new MemoryStream(fileModel.data);

                Document mydoc = DocumentBuilder.NewDocumentNamed(fileModel.displayName).FromStream(stream, DocumentType.PDF).Build();
                eslClient.UploadDocument(mydoc, pkg);
                return mydoc.Id;
Here is the error: Silanis.ESL.SDK.EslServerException was caught HResult=-2146233088 Message=Could not upload document to package. Exception: The remote server returned an error: (400) Bad Request. HTTP POST on URI https://sandbox.e-signlive.com/api/packages/e0ca4410-4637-450d-9b3d-5f1f08efdf0e/documents. Optional details: {"entity":null,"technical":"Unable to verify document fc4a3015ad9fd37e. Please make sure it is a valid document or save it as a different format. Error: PDF header signature not found.","packageId":null,"messageKey":"error.validation.verifyDocument.validDocument","message":"Unable to verify document. Please make sure it is a valid document or save it in a different format.","code":400,"name":"Validation Error"} Source=Silanis.ESL StackTrace: at Silanis.ESL.SDK.Services.PackageService.UploadDocument(DocumentPackage package, String fileName, Byte[] fileBytes, Document document) at Silanis.ESL.SDK.EslClient.UploadDocument(String fileName, Byte[] fileContent, Document document, DocumentPackage documentPackage) at Silanis.ESL.SDK.EslClient.UploadDocument(Document document, DocumentPackage documentPackage) at BoardBookitWebService.Repositories.SignatureRepository.addEslApprovalDocument(Int32 companyId, String packageId, FileModel fileModel) in c:\Projects\BoardBookit-Development_Branch\BoardBookitWebService\Repositories\SignatureRepository.cs:line 96 InnerException: Silanis.ESL.SDK.EslServerException HResult=-2146233088 Message=The remote server returned an error: (400) Bad Request. HTTP POST on URI https://sandbox.e-signlive.com/api/packages/e0ca4410-4637-450d-9b3d-5f1f08efdf0e/documents. Optional details: {"entity":null,"technical":"Unable to verify document fc4a3015ad9fd37e. Please make sure it is a valid document or save it as a different format. Error: PDF header signature not found.","packageId":null,"messageKey":"error.validation.verifyDocument.validDocument","message":"Unable to verify document. Please make sure it is a valid document or save it in a different format.","code":400,"name":"Validation Error"} Source=Silanis.ESL StackTrace: at Silanis.ESL.SDK.Internal.HttpMethods.MultipartPostHttp(String apiToken, String path, Byte[] content, String boundary, AuthHeaderGenerator authHeaderGen) at Silanis.ESL.SDK.RestClient.PostMultipartFile(String path, Byte[] fileBytes, String boundary, String json) at Silanis.ESL.SDK.Services.PackageService.UploadDocument(DocumentPackage package, String fileName, Byte[] fileBytes, Document document) InnerException: System.Net.WebException HResult=-2146233079 Message=The remote server returned an error: (400) Bad Request. Source=System StackTrace: at System.Net.HttpWebRequest.GetResponse() at Silanis.ESL.SDK.Internal.HttpMethods.MultipartPostHttp(String apiToken, String path, Byte[] content, String boundary, AuthHeaderGenerator authHeaderGen) InnerException: Thank you, Colleen

Reply to: Adding documents from database

0 votes
It seems that your PDF is missing a header. Can you try opening your PDF with notepad and verify that the first line is for example: %PDF-1.5. Anything else would mean that your PDF is corrupted and that you will have to regenerate it.
Haris Haidary OneSpan Technical Consultant

Reply to: Adding documents from database

0 votes
Haris, I did what you suggested and opened the attached pdf in notepad. The first few characters are this: %PDF-1.5 I have tried this process with a few different pdf files that we use in testing that all work and load as a PDF in other parts of our application. We are uploading the file via HttpPostedFileBase and they saving the content and content type into the database. I just want to pass along the data to eSign Live and add the same file there. We never save the file to disk. Any other suggestions? Colleen

Attachments

Reply to: Adding documents from database

0 votes
I will investigate this issue with our maintenance team and get back to you when I have an update. We will need to reproduce this on our end in order to further assist you. Could you also let me know what SDK version you are using?
Haris Haidary OneSpan Technical Consultant

Reply to: Adding documents from database

0 votes
Haris, I am using 10.13.0.0 of the SDK. I tried a couple of things on my end. It looks like I need the byte array from the database and not the byte array from the posted file. I saved the posted file to the database first. Then retrieved it and passed it to the uploaddocument call. The file uploaded successfully. I didn't realize that I couldn't just use the byte array from the posted file. Seems to be working fine now. Thank you for the help! Colleen

Reply to: Adding documents from database

0 votes
Glad you were able to resolve the issue :)
Haris Haidary OneSpan Technical Consultant

Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off