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>