on ‎2023 Jun 07 11:36 AM
Hello All,
We are trying to post a document in BTP DMS using BTP DMS API's from our nodejs CAPM project but unfortunately it creates a document in BTP DMS with corrupted content so unable to read it.Attaching screenshot for reference -


We tried passing the document as bas64string, unitArray, buffer, PDFdoument but nothing works.It creates the document without giving any error but created document is not readable.Below is the nodejs code snippet -
async function _createDocument(sFolderName, folderCreateURL, filedata) {
var formData = new FormData();
// formData.append("objectid", sFolderName);
formData.append("cmisaction", "createDocument");
formData.append("propertyId[0]", "cmis:name");
formData.append("propertyId[1]", "cmis:objectTypeId");
formData.append("propertyValue[1]", "cmis:document");
formData.append("_charset_", "UTF-8");
formData.append("includeAllowableActions", "true");
formData.append("succinct", "true");
formData.append("propertyValue[0]", filedata.fileName);
formData.append("filename", filedata.fileName);
formData.append("media", filedata.fileAsBase64); //passing document as base64 string .Also tried passing it as Buffer, UnitArray and PDFDocument
let headers = formData.getHeaders();
const config = {
headers: headers
}
var that = this;
const docresponse = await axiosCf({
method: "post",
url: folderCreateURL,
headers: config.headers,
data: formData
}).then(async (result) => {
console.log('Attachment uploaded successfully');
that.docresponse = result.data.succinctProperties["cmis:objectId"];
}).catch(error => {
console.log("Attachement upload failed - " + error);
that.docresponse = "";
});
}
However, when we test it using postman client which is passing the document as FileObject, it worksPostmanScreenshot -

Does anyone with any idea on how to resolve this issue?
BTP DMS Rest API link used for creating document for reference - https://api.sap.com/api/CreateDocumentApi/path/post_browser__repository_id__root__directory_path_
Regards,Jigar Salecha
Request clarification before answering.
Hi Jigar,
FormData expects raw binary (a Buffer or Blob), not base64 strings, hence
Instead of
formData.append("media", filedata.fileAsBase64); //passing document as base64 string .Also tried passing it as Buffer, UnitArray and PDFDocumentlet us use:-
const fileBuffer = fs.readFileSync(filedata.filePath);
formData.append("media", fileBuffer, {
filename: filedata.fileName,
contentType: "application/pdf" // adjust this based on file type
});which you had already tried, hope it is in the above pattern.
In Postman it is working because Postman handles file uploads correctly using multipart (as soon as you select Body > form-data and use a File type field)
Helpful read, link
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 9 | |
| 7 | |
| 6 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.