Select to view content in your preferred language

Adding an attachment with a feature being created

03-30-2023 12:34 PM
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: [{
contentType: file.type,
addFeatures: [newFeature]
}).then(function (results) {
addAttachments: [newFeature]
}).then(function (results) {






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
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" });
    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,


0 Kudos