cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Issue in creating document using SAP BTP DMS API'S from CAPM nodejs project

jigar_salecha
Explorer
3,506

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

Accepted Solutions (0)

Answers (2)

Answers (2)

ArunJacob
Active Participant
0 Likes

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 PDFDocument

let 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

 

Ajit_K_Panda
Product and Topic Expert
Product and Topic Expert
0 Likes
renuka23
Explorer
0 Likes
This document is related to folder creation, but I want to upload the file to DMS using code where we are facing issues with file format