Add attachment with javascript api 4.x

2197
5
08-02-2018 07:53 PM
JamesMoloney2
New Contributor II

Hi all

I have a feature layer that has attachments. 

Using the javascript api I can query the feature layer for attachments using the queryFeatureAttachments method

eg 

fl.queryFeatureAttachments(feature).then(function(attachments){
 for (var x = 0, xl = info.length; x < xl; x++) {
   console.log(attachments[x]);
  };
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

So I now have the attachments and I can log them to console.

My question is how do I attach these attachments to a feature in another feature service?

There is no method in the javascript api 4.x to add an attachment 

Has anyone worked out a way to do this? I was thinking of using the rest api Add Attachment—ArcGIS REST API: Services Directory | ArcGIS for Developers but I'm not sure what the post object should look like?

Any help here would be much appreciated

Thanks

James

0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus

James,

   This thread is from 3.x API but it is using the REST service to add the attachement so I should work in 4.x:

https://community.esri.com/thread/107648 

0 Kudos
JamesMoloney2
New Contributor II

Hi Robert,

Thanks for pointing me to the post above...

This is what I have so far,

fl.queryFeatureAttachments(feature).then(function(attachments){                                                        
                    console.log(attachments);

                         for (var x = 0, xl = attachments.length; x < xl; x++) {
                                console.log(attachments[x]);
                                var contentType = attachments[x].contentType;
                                var contentName = attachments[x].name;

                                var xhr = new XMLHttpRequest();
                                xhr.open("GET",attachments[x].url,true);
                                xhr.responseType = "arraybuffer";

                                xhr.onload = function(evt){
                                     console.log(this.response);
                                   var arrayBufferView = new Uint8Array( this.response );
                                   var blob = new Blob([ arrayBufferView ], { type: contentType });
                                   var formData = new FormData();
                                   formData.append("method", "post")
                                   formData.append("enctype", "multipart/form-data");
                                   formData.append("attachment", blob, contentName);
                                   console.log(formData);
                                    esriRequest({  
                                      url: "https://services8.arcgis.com/[AGO]/arcgis/rest/services/[fl]/FeatureServer/0/[objectid]/addAttachmen...",  
                                      form: formData,  
                                      content: {  
                                          f: "json"},  
                                      handleAs: "json"  
                             }).then(function (response){  
                                 console.log(response);
                                 });                                      
                                
                                };
                                xhr.send(contentType,contentName);
                         };
                    });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

but I am getting this error 

dojo.js:412 Uncaught TypeError: d.slice is not a function
at Object.G [as isBlobProtocol] (dojo.js:412)
at dojo.js:452
at XMLHttpRequest.xhr.onload (gch.js:331)

Any ideas what this means?

Thanks James

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

James,

  Sorry I do not.  Maybe Rene Rubalcava‌ can help.

0 Kudos
JohnGrayson
Esri Regular Contributor

I would just use esriRequest instead of XMLHttpRequest, and also please note that the signature for esriRequest is different at 4.x so it won't work as you have it above: Esri Request | API Reference 

SamHall
New Contributor II

I am getting the same error. Any update on this original issue?

0 Kudos