Adding an attachment with a feature being created

252
1
03-30-2023 12:34 PM
KyleSchultz1
New Contributor II
function saveNewFeature() 
{
var reader = new FileReader();
var file = inputphoto.files[0];
reader.addEventListener('load', function (event) {
var newFeature = ({
attributes: {
"brochure": inputBrochure.value,
"meterNumber": inputmeterNumber.value,
"materialIn": inputmaterialIn.value,
"materialOut": inputmaterialOut.value,
"dateCreated": inputdateCreated.value,
"svNumber": inputsvNumber.value,
"address": selectedFeature.graphic.attributes.USPS_LSN,
"scheduledAppoint": inputscheduledAppoint.value,
"notes": inputnotes.value
},
geometry: selectedFeature.graphic.geometry,
attachments: [{
name: file.name,
contentType: file.type,
data: event.target.result
}]
});
console.log(newFeature)
globalMaterial.applyEdits({
addFeatures: [newFeature]
}).then(function (results) {
globalMaterial.applyEdits({
addAttachments: [newFeature]
}).then(function (results) {
console.log(results)

});
});
});

reader.readAsArrayBuffer(file);

}

 

 

Forgive me I'm new to arcgis js library. Trying to create a new feature with an existing geometry and attributes. The feature is successfully created but the attachment is left empty. I can add it through AGOL.  Any suggestions?

0 Kudos
1 Reply
UndralBatsukh
Esri Regular Contributor

Hi there, 

Setting attachments directly on the Feature/Graphic object does not have attachments property. If your service supports globalIds then you can use the addAttachments parameter on FeatureLayer.applyEdits. The globalIdUsed parameter for applyEdits method has a code snippet for this. I copied and pasted it here just case. You can modify it so that you are creating and adding attachments with one applyEdits.

function addAttachment(selectedFeature) {
  const blob = new Blob(byteArrays, { type: "image/png" });
  addAttachments.push({
    feature: selectedFeature,
    attachment: {
      globalId: "8c4d6085-a33c-42a0-8e11-21e9528bca0d",
      name: "brokenLight",
      data: blob
    }
  });

  const edits = {
    addAttachments: addAttachments
  };

  const options = {
    // globalIdUsed has to be true when adding, updating or deleting attachments
    globalIdUsed: true,
    rollbackOnFailureEnabled: true
  };

  featureLayer.applyEdits(edits, options).then(function(results) {
    console.log("edits added: ", results);
  });
}

 

Or you can use FeatureLayer.addAttachment method to add attachments to your feature. In this case, you will have to create the feature first, get the feature once it is created then call addAttachment method. 

Hope this helps,

-Undral

0 Kudos