<?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 Uploading Image Attachments to Hosted Table Layer in ArcGIS Online using JS API in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/uploading-image-attachments-to-hosted-table-layer/m-p/1269760#M80614</link>
    <description>&lt;P&gt;Hi, I have tried to write a simple HTML form to add a new record/feature as well as Image Attachment upload into a hosted table layer in ArcGIS Online using ArcGIS JS API, the code as follow:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8"&amp;gt;
    &amp;lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"&amp;gt;
    &amp;lt;title&amp;gt;Feature Form&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.21/esri/themes/light/main.css"&amp;gt;
    &amp;lt;style&amp;gt;
      body {
        margin: 0;
        padding: 0;
      }
      #form-container {
        padding: 16px;
      }
      #form-container label {
        display: block;
        margin-bottom: 8px;
      }
      #form-container input[type="text"],
      #form-container input[type="number"],
      #form-container textarea {
        width: 100%;
        padding: 8px;
        margin-bottom: 16px;
        box-sizing: border-box;
        border: 1px solid #ccc;
        border-radius: 4px;
        resize: vertical;
      }
      #form-container input[type="file"] {
        margin-top: 16px;
      }
      #form-container button[type="submit"] {
        background-color: #4CAF50;
        color: white;
        padding: 8px 16px;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        float: right;
      }
      #form-container button[type="submit"]:hover {
        background-color: #45a049;
      }
    &amp;lt;/style&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div id="form-container"&amp;gt;
      &amp;lt;form id="feature-form"&amp;gt;
       &amp;lt;label for="id" class="input-label"&amp;gt;ID Rusun:&amp;lt;/label&amp;gt;
     &amp;lt;input type="text" id="id" name="id" value="JTGRSN22-04" readonly&amp;gt;&amp;lt;br&amp;gt;

         &amp;lt;label for="total_wakt"&amp;gt;Total Waktu:&amp;lt;/label&amp;gt;
			&amp;lt;input type="text" id="total_wakt" name="total_wakt"&amp;gt;&amp;lt;br&amp;gt;


        &amp;lt;label for="file-input"&amp;gt;Attach Image:&amp;lt;/label&amp;gt;
        &amp;lt;input type="file" id="file-input" name="file-input"&amp;gt;

        &amp;lt;button type="submit" id="submit-button"&amp;gt;Submit&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;!-- ArcGIS JavaScript API --&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.9/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;!-- Your JavaScript code goes here --&amp;gt;
    &amp;lt;script&amp;gt;
	require([
  "esri/layers/FeatureLayer",
  "esri/Graphic",
  "esri/widgets/Popup",
  "esri/PopupTemplate",
  "esri/widgets/FeatureForm",
  "dojo/dom",
  "dojo/domReady!"
], function (
  FeatureLayer,
  Graphic,
  Popup,
  PopupTemplate,
  FeatureForm,
  dom
) {

  // Create a new feature layer for the hosted table with attachments enabled
  const featureLayer = new FeatureLayer({
    url: "https://services-ap1.arcgis.com/p2UXWyaOSPcjxuJd/arcgis/rest/services/progres_tes/FeatureServer/1",
    outFields: ["*"],
    visible: false,
    supportsAttachments: true
  });

  // Create a new popup template for the feature layer
  const popupTemplate = new PopupTemplate({
    title: "{id}",
    content: [
      {
        type: "fields",
        fieldInfos: [
          { fieldName: "id", label: "ID Rusun" },
          { fieldName: "total_wakt", label: "Total Waktu" }
         
        ]
      },
      {
        type: "attachments"
      }
    ]
  });

  // Attach the popup template to the feature layer
  featureLayer.popupTemplate = popupTemplate;

  // Create a new feature form to handle form inputs
  const featureForm = new FeatureForm({
    container: "form",
    layer: featureLayer
  });
  
  
 // Handle form submission
  const submitButton = dom.byId("submit-button");
  submitButton.addEventListener("click", function (event) {
    event.preventDefault(); // Prevent form submission
    // Get the feature form's values
    const values = featureForm.getValues();
   
    // Create a new graphic with the form values
    const graphic = new Graphic({
      geometry: null,
      attributes: values
    });
    // Add the graphic to the feature layer
    featureLayer.applyEdits({
      addFeatures: [graphic]
    }).then(function (addResults) {
      // Get the object ID of the newly added feature
      const objectId = addResults[0].objectId;
      // Get the file input element and the selected file
      const fileInput = dom.byId("file-input");
      const file = fileInput.files[0];
      // Use the addAttachment method to add the attachment to the feature
      featureLayer.addAttachment(objectId, file).then(function () {
        alert("Feature added successfully");
      }).catch(function (error) {
        alert("Error adding attachment: " + error.message);
      });
    }).catch(function (error) {
      alert("Error adding feature: " + error.message);
    });
  });
});

	&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;When I tried to run the code, I got error message:&amp;nbsp;&lt;SPAN class=""&gt;: &lt;/SPAN&gt;&lt;SPAN class=""&gt;"The table feature service type is not yet supported" in the browser console,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;I am not sure if the operation performed in the code is really not supported because I can edit the feature and upload image attachment directly inside the ArcGIS Online Map Viewer,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;any helps or hints would be appreciated, thank you so much.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Mar 2023 07:28:25 GMT</pubDate>
    <dc:creator>BramantiyoMarjuki</dc:creator>
    <dc:date>2023-03-21T07:28:25Z</dc:date>
    <item>
      <title>Uploading Image Attachments to Hosted Table Layer in ArcGIS Online using JS API</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/uploading-image-attachments-to-hosted-table-layer/m-p/1269760#M80614</link>
      <description>&lt;P&gt;Hi, I have tried to write a simple HTML form to add a new record/feature as well as Image Attachment upload into a hosted table layer in ArcGIS Online using ArcGIS JS API, the code as follow:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8"&amp;gt;
    &amp;lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"&amp;gt;
    &amp;lt;title&amp;gt;Feature Form&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.21/esri/themes/light/main.css"&amp;gt;
    &amp;lt;style&amp;gt;
      body {
        margin: 0;
        padding: 0;
      }
      #form-container {
        padding: 16px;
      }
      #form-container label {
        display: block;
        margin-bottom: 8px;
      }
      #form-container input[type="text"],
      #form-container input[type="number"],
      #form-container textarea {
        width: 100%;
        padding: 8px;
        margin-bottom: 16px;
        box-sizing: border-box;
        border: 1px solid #ccc;
        border-radius: 4px;
        resize: vertical;
      }
      #form-container input[type="file"] {
        margin-top: 16px;
      }
      #form-container button[type="submit"] {
        background-color: #4CAF50;
        color: white;
        padding: 8px 16px;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        float: right;
      }
      #form-container button[type="submit"]:hover {
        background-color: #45a049;
      }
    &amp;lt;/style&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div id="form-container"&amp;gt;
      &amp;lt;form id="feature-form"&amp;gt;
       &amp;lt;label for="id" class="input-label"&amp;gt;ID Rusun:&amp;lt;/label&amp;gt;
     &amp;lt;input type="text" id="id" name="id" value="JTGRSN22-04" readonly&amp;gt;&amp;lt;br&amp;gt;

         &amp;lt;label for="total_wakt"&amp;gt;Total Waktu:&amp;lt;/label&amp;gt;
			&amp;lt;input type="text" id="total_wakt" name="total_wakt"&amp;gt;&amp;lt;br&amp;gt;


        &amp;lt;label for="file-input"&amp;gt;Attach Image:&amp;lt;/label&amp;gt;
        &amp;lt;input type="file" id="file-input" name="file-input"&amp;gt;

        &amp;lt;button type="submit" id="submit-button"&amp;gt;Submit&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;!-- ArcGIS JavaScript API --&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.9/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;!-- Your JavaScript code goes here --&amp;gt;
    &amp;lt;script&amp;gt;
	require([
  "esri/layers/FeatureLayer",
  "esri/Graphic",
  "esri/widgets/Popup",
  "esri/PopupTemplate",
  "esri/widgets/FeatureForm",
  "dojo/dom",
  "dojo/domReady!"
], function (
  FeatureLayer,
  Graphic,
  Popup,
  PopupTemplate,
  FeatureForm,
  dom
) {

  // Create a new feature layer for the hosted table with attachments enabled
  const featureLayer = new FeatureLayer({
    url: "https://services-ap1.arcgis.com/p2UXWyaOSPcjxuJd/arcgis/rest/services/progres_tes/FeatureServer/1",
    outFields: ["*"],
    visible: false,
    supportsAttachments: true
  });

  // Create a new popup template for the feature layer
  const popupTemplate = new PopupTemplate({
    title: "{id}",
    content: [
      {
        type: "fields",
        fieldInfos: [
          { fieldName: "id", label: "ID Rusun" },
          { fieldName: "total_wakt", label: "Total Waktu" }
         
        ]
      },
      {
        type: "attachments"
      }
    ]
  });

  // Attach the popup template to the feature layer
  featureLayer.popupTemplate = popupTemplate;

  // Create a new feature form to handle form inputs
  const featureForm = new FeatureForm({
    container: "form",
    layer: featureLayer
  });
  
  
 // Handle form submission
  const submitButton = dom.byId("submit-button");
  submitButton.addEventListener("click", function (event) {
    event.preventDefault(); // Prevent form submission
    // Get the feature form's values
    const values = featureForm.getValues();
   
    // Create a new graphic with the form values
    const graphic = new Graphic({
      geometry: null,
      attributes: values
    });
    // Add the graphic to the feature layer
    featureLayer.applyEdits({
      addFeatures: [graphic]
    }).then(function (addResults) {
      // Get the object ID of the newly added feature
      const objectId = addResults[0].objectId;
      // Get the file input element and the selected file
      const fileInput = dom.byId("file-input");
      const file = fileInput.files[0];
      // Use the addAttachment method to add the attachment to the feature
      featureLayer.addAttachment(objectId, file).then(function () {
        alert("Feature added successfully");
      }).catch(function (error) {
        alert("Error adding attachment: " + error.message);
      });
    }).catch(function (error) {
      alert("Error adding feature: " + error.message);
    });
  });
});

	&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;When I tried to run the code, I got error message:&amp;nbsp;&lt;SPAN class=""&gt;: &lt;/SPAN&gt;&lt;SPAN class=""&gt;"The table feature service type is not yet supported" in the browser console,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;I am not sure if the operation performed in the code is really not supported because I can edit the feature and upload image attachment directly inside the ArcGIS Online Map Viewer,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;any helps or hints would be appreciated, thank you so much.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 07:28:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/uploading-image-attachments-to-hosted-table-layer/m-p/1269760#M80614</guid>
      <dc:creator>BramantiyoMarjuki</dc:creator>
      <dc:date>2023-03-21T07:28:25Z</dc:date>
    </item>
  </channel>
</rss>

