Hi,
I'm trying to add an attachment via a REST request in Node JS but it give me an error with code 400 and message "Unable to add attachment to the feature.". The layer is hosted in AGOL and has attachments enabled. Via AGOL I can add and delete attachments without any issues. I'm using form-data to add the attachment on the REST request.
Screenshot of error:
Code Snippet:
/**
* Adds an attachment to the feature service layer.
*
* @param {esri/arcgis-rest-request/IAuthenticationManager} authManager -
* An object that implements IAuthenticationManager and manages the
* session.
* @param {int} attachmentRecordObjectid - The object id of the record in
* which the attachment wil be added.
* @param {string} image - The path to the image to be added as an
* attachment.
* @return {Promise<Object>} An object with an objectId property that
* contains the objectId value of the added attachment, null if
* attachment was not added.
*/
async _addAttachmentImage(authManager, attachmentRecordObjectid, image, keyword) {
return new Promise(async function (resolve, reject) {
try {
let objectId = null;
const token = await authManager.getToken(this.attachmentSeviceUrl);
const imageFormData = new FormData();
const myFile = fs.createReadStream(image, {encoding:'UTF-8'});
imageFormData.append('attachment', myFile, {
filename: 'test.png',
contentType: 'image/png'
});
imageFormData.append('enctype', 'multipart/form-data');
//Url Sample:
//https://services5.arcgis.com/dfdgfdrgrrg/arcgis/rest/
//services/MyLayerName/FeatureServer/1/1/addAttachment
const attachmentUrl =
`${this.attachmentSeviceUrl}/${attachmentRecordObjectid}/addAttachment`;
const bodyParams = {
attachment: imageFormData
};
const options = {
params: {
token: token,
f: 'json'
},
headers: {
'Content-Type': 'multipart/form-data'
}
};
const res = await axios.post(attachmentUrl, bodyParams, options);
console.log(res);
if (res.data.addAttachmentResult && res.data.addAttachmentResult.objectId)
{
objectId = { objectId: res.data.addAttachmentResult.objectId };
}
console.log(`Attachment Image ObjectID: ${objectId}`);
resolve(objectId);
}
catch(e) {
console.log(e);
reject(e);
}
}.bind(this));
}
Any ideas of what is wrong in the request that is causing the error "Unable to add attachment to the feature"?
Solved! Go to Solution.
Hi Karen.
I solved the problem by using the following code:
/**
* Adds an attachment to the feature service layer.
*
* @param {esri/arcgis-rest-request/IAuthenticationManager} authManager -
* An object that implements IAuthenticationManager and manages the
* session.
* @param {int} attachmentRecordObjectid - The object id of the record in
* which the attachment wil be added.
* @param {string} image - The path to the image to be added as an
* attachment.
* @return {Promise<Object>} An object with an objectId property that
* contains the objectId value of the added attachment, null if
* attachment was not added.
*/
async _addAttachmentImage(authManager, attachmentRecordObjectid, image,
keyword) {
return new Promise(async function (resolve, reject) {
try {
let objectId = null;
const token = await
authManager.getToken(this.attachmentSeviceUrl);
const imageFormData = new FormData();
imageFormData.append('attachment',
fs.createReadStream(image));
const formHeaders = imageFormData.getHeaders();
//Url Sample:
//https://services5.arcgis.com/dfdgfdrgrrg/arcgis/rest/
//services/MyLayerName/FeatureServer/1/1/addAttachment
const attachmentUrl =
`${this.attachmentSeviceUrl}/${attachmentRecordObjectid}/addAttachment`;
const options = {
params: {
token: token,
keywords: keyword,
f: 'json'
},
headers: {
...formHeaders
}
};
const res = await axios.post(attachmentUrl, imageFormData,
options);
console.log(res);
if (res.data.error) {
reject(res.data.error);
}
if (res.data.addAttachmentResult &&
res.data.addAttachmentResult.objectId)
{
objectId = { objectId:
res.data.addAttachmentResult.objectId };
}
console.log(`Attachment Image ObjectID: ${objectId}`);
resolve(objectId);
}
catch(e) {
console.log(e);
reject(e);
}
}.bind(this));
}
Note that for this solution I'm using the following modules:
Do you resolve this problem? I'm facing the same. I've been reading the documentation for a while but there is nothing...
I'll be glad to be helped.
Thank you.
Hi Karen.
I solved the problem by using the following code:
/**
* Adds an attachment to the feature service layer.
*
* @param {esri/arcgis-rest-request/IAuthenticationManager} authManager -
* An object that implements IAuthenticationManager and manages the
* session.
* @param {int} attachmentRecordObjectid - The object id of the record in
* which the attachment wil be added.
* @param {string} image - The path to the image to be added as an
* attachment.
* @return {Promise<Object>} An object with an objectId property that
* contains the objectId value of the added attachment, null if
* attachment was not added.
*/
async _addAttachmentImage(authManager, attachmentRecordObjectid, image,
keyword) {
return new Promise(async function (resolve, reject) {
try {
let objectId = null;
const token = await
authManager.getToken(this.attachmentSeviceUrl);
const imageFormData = new FormData();
imageFormData.append('attachment',
fs.createReadStream(image));
const formHeaders = imageFormData.getHeaders();
//Url Sample:
//https://services5.arcgis.com/dfdgfdrgrrg/arcgis/rest/
//services/MyLayerName/FeatureServer/1/1/addAttachment
const attachmentUrl =
`${this.attachmentSeviceUrl}/${attachmentRecordObjectid}/addAttachment`;
const options = {
params: {
token: token,
keywords: keyword,
f: 'json'
},
headers: {
...formHeaders
}
};
const res = await axios.post(attachmentUrl, imageFormData,
options);
console.log(res);
if (res.data.error) {
reject(res.data.error);
}
if (res.data.addAttachmentResult &&
res.data.addAttachmentResult.objectId)
{
objectId = { objectId:
res.data.addAttachmentResult.objectId };
}
console.log(`Attachment Image ObjectID: ${objectId}`);
resolve(objectId);
}
catch(e) {
console.log(e);
reject(e);
}
}.bind(this));
}
Note that for this solution I'm using the following modules: