<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: applyEdits() addAttachments - optional file/image attachments? in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/applyedits-addattachments-optional-file-image/m-p/1189507#M77847</link>
    <description>&lt;P&gt;Ended up going this route and it looks like its working just fine...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const attachmentOne = document.getElementById("photoAttach1");
 obsLayer.addAttachment(editFeature, attachmentOne).then((result) =&amp;gt; {
   console.log("attachment added: ", result);
 }).catch(function (err) {
   console.log("no attachment found or error: ", err);
 });

const attachmentTwo = document.getElementById("photoAttach2");
 obsLayer.addAttachment(editFeature, attachmentTwo).then((result) =&amp;gt; {
  console.log("attachment added: ", result);
 }).catch(function (err) {
  console.log("no attachment found or error: ", err);
 });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 05 Jul 2022 17:09:43 GMT</pubDate>
    <dc:creator>luckachi</dc:creator>
    <dc:date>2022-07-05T17:09:43Z</dc:date>
    <item>
      <title>applyEdits() addAttachments - optional file/image attachments?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/applyedits-addattachments-optional-file-image/m-p/1186716#M77775</link>
      <description>&lt;P&gt;As part of my featureForm.on(submit) event, I have a piece of code that grab files from an HTML file input, creates a blob and then adds them as attachments to the record. However, I can't seem to correctly handle the addAttachments process when no files are selected. When there are no files selected, the applyEdits() addAttachments still fires and adds 2 undefined ( dW5kZWZpbmVk) blobs that have a file size of 9, which ends up being the length of the type 'image/jpg'. When I tweak the code to be an if else statement based on the files.length, in the console log I receive an error that&amp;nbsp;&lt;STRONG&gt;"Attachment should have 'data' or 'uploadId'"&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;/********
** Feature Form Submit Process
********/

        // Listen to the feature form's submit event. Update feature attributes shown in the form.
        featureForm.on("submit", () =&amp;gt; {
          if (editFeature) {

            // Grab updated attributes from the form.
            const updated = featureForm.getValues();

            // Loop through updated attributes and assign the updated values to feature attributes.
            Object.keys(updated).forEach((name) =&amp;gt; {
              editFeature.attributes[name] = updated[name];
            });

            // grabs the image file and converts it to a blob (req.) for addAttachments to process
            const blob = new Blob([document.getElementById("photo").files[0]]); // Photo 1

            const blobTwo = new Blob([document.getElementById("photoTwo").files[0]], {type: "image/jpeg"}); // Photo 2

            if (document.getElementById("photo").files.length === 0) {
              console.log("no files selected");
            }

            if (document.getElementById("photoTwo").files.length === 0) {
              console.log("no files selected");
            }

            // When you submit a record with no photos, two attachments still get loaded
            // a white square with 9 bytes?

            // // First Photo
            const attachments = {
              feature: editFeature,
              attachment: {
                globalId: editFeature.attributes.GlobalID,
                name: "attachment_OBJECTID_" + editFeature.attributes.OBJECTID,
                contentType: "image/jpeg",
                data: blob
              }
            }

            // // Second Photo
            const attachmentsTwo = {
              feature: editFeature,
              attachment: {
                globalId: editFeature.attributes.GlobalID,
                name: "attachment_OBJECTID_" + editFeature.attributes.OBJECTID,
                data: blobTwo
              }
            }

            // Setup the applyEdits parameter with updates.
            const edits = {
              updateFeatures: [editFeature],
              // addAttachments: [attachments]
              addAttachments: [attachments, attachmentsTwo]
            };

            const options = {
              globalIdUsed: true, // has to be true when adding, updating or deleting attachments
              rollbackOnFailureEnabled: false // has to be true or at least I think so
            };

            console.log(edits);
            applyEditsToIncidents(edits, options);
            document.getElementById("viewDiv").style.cursor = "auto";
          }
        });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 13:58:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/applyedits-addattachments-optional-file-image/m-p/1186716#M77775</guid>
      <dc:creator>luckachi</dc:creator>
      <dc:date>2022-06-27T13:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: applyEdits() addAttachments - optional file/image attachments?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/applyedits-addattachments-optional-file-image/m-p/1189507#M77847</link>
      <description>&lt;P&gt;Ended up going this route and it looks like its working just fine...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const attachmentOne = document.getElementById("photoAttach1");
 obsLayer.addAttachment(editFeature, attachmentOne).then((result) =&amp;gt; {
   console.log("attachment added: ", result);
 }).catch(function (err) {
   console.log("no attachment found or error: ", err);
 });

const attachmentTwo = document.getElementById("photoAttach2");
 obsLayer.addAttachment(editFeature, attachmentTwo).then((result) =&amp;gt; {
  console.log("attachment added: ", result);
 }).catch(function (err) {
  console.log("no attachment found or error: ", err);
 });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 17:09:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/applyedits-addattachments-optional-file-image/m-p/1189507#M77847</guid>
      <dc:creator>luckachi</dc:creator>
      <dc:date>2022-07-05T17:09:43Z</dc:date>
    </item>
  </channel>
</rss>

