This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal


Sep 24, 2015, 11:05 AM
1 Posts

How to upload attachment with Domino Data Services

  • Category: Application Development
  • Platform: Windows
  • Release: 9.0.1
  • Role: Developer
  • Tags: Domino Data Services,jQuery,Attachment,JavaScript
  • Replies: 2

I have tried the code below in an html file resource in Domino Designer, to upload an attachment to a new document in the current Domino database, whenever the user selects a file in the file upload control. There is no Domino form in the design of the database. The JavaScript file API is supported in my browser (Google Chrome Version 45.0.2454.99 m). I am using Domino Server 9.0.1.

A document is created, with a Body field containing MIME, and a $FILE field apparently containing the attached file. Yet when trying to download the file, the server finds no file. My link is like

<a target="_blank" href="https://domain/path/database/view/document unid/$FILE/attachment name">attachment name</a>

When I GET the document with Domino Data Services, the Body field has the binary content of the attachment corrupted. The multipart MIME structure of Body matches that of attachments created successfully by a Java agent.

It seems the binary data is being corrupted in transit to/from the server.

I have tried converting the binary data to a hexadecimal representation (as ASCII text) and converting back when read, but the input does not match the original. The same happened with Base64 encoding.

I have also tried using 'processData: false' in the jQuery Ajax call, to no avail.

I want to upload an attachment, which is correctly stored in $FILE, and can be downloaded from a web page with the link as above.

Please can anyone help me.

 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"  content="width=device-width, initial-scale=1">
    <title>Upload</title>    
    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
      function uploadAttachment(file,content){
        obj3 = {
                table: 'file',
                Body: {
                       type: 'multipart',
                       content: [{
                                  contentType: file.type,
                                  contentDisposition: ('attachment;filename="' + file.name + '"'),
                                  data: content
                                }]
                      }
               };
        $.ajax({
                type: 'POST',
                async: true,
                cache: false,
                url: (window.baseUrl1 + 'api/data/documents'),
                data: JSON.stringify(obj3),
                dataType: 'xml',
                accepts: {
                            xml: 'text/xml',
                            text: 'text/plain'
                },
                contentType: 'application/json',
                success: function(data2,textStatus2,jqxhr2){
                  console.log(textStatus2);
                },
                error: function(jqxhr2,textStatus2,errorThrown2){
                    console.log(textStatus2 + ' ' + errorThrown2);
                }
        });
      };

      function readBlob(){
        var files1 = document.getElementById('file1').files;
        if ((!files1.length) || (files1.length < 1)){
          return;
        };
        var file1 = files1[0];
        var reader1 = new FileReader();
        reader1.onloadend = function(evt){
          if (evt.target.readyState == FileReader.DONE){
            uploadAttachment(file1,evt.target.result);
          };
        };
        var blob1 = file1.slice(0,file1.size);
        reader1.readAsBinaryString(blob1);
      };
      window.baseUrl1 = (window.location.href.replace(/(^.*\.nsf)(.*$)/i,'$1') + '/'); //Includes trailing / character.
      
      $(window).load(function(){
        $('#file1').change(function(){readBlob();});
      });
    </script>
  </head>
  <body>
    <input type="file" id="file1" name="file1" />
  </body>

Sep 24, 2015, 11:35 PM
43 Posts
I think the documentation stated that attachment would not be sent

At least that is what I remembered when reading it a few month ago.

Sep 25, 2015, 7:10 PM
19 Posts
Content transfer encoding
Did you try specifying the content transfer encoding?  

Base64 encoding is recommended -- especially for binary data.  You say you tried Base64 encoding, but did you also include the contentTransferEncoding property in your JSON input?  I just tried the following POST request using the REST Client for Firefox.  In my case, the {database} segment in the URL is just a mail file, but it should work for any type of database.

-- Dave


POST /{database}/api/data/documents?form=Memo
Content-Type: application/json

{
  "Subject": "Sample file attachment",
  "Body": {
    "type": "multipart",
    "content": [
      {
        "contentType": "application/octet-stream; name=\"file.dat\"",
        "contentDisposition": "attachment; filename=\"file.dat\"",
        "contentTransferEncoding": "base64",
        "data": "UmFuZG9tIGZpbGUgZGF0YQ=="
      }
    ]
  }
}

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal