AnsweredAssumed Answered

Problem using AddAttachment REST method in C#

Question asked by Marvin_84 on Mar 22, 2017
Latest reply on Feb 6, 2018 by clavoro_BCGIS

Hi,

I'm trying to attach a file to feature in a Esri Feature Service using the REST methods in a Visual Studio C# program, the feature already exist; so the next step as I understand is to call the "AddAttachment" method and pass the parameters, however I'm getting an error sending the file I want to attach, the error message says the following:

 

{"error":{"code":500,"message":"com.esri.arcgis.discovery.json.JSONArray cannot be cast to com.esri.arcgis.discovery.json.JSONObject","details":[]}}

 

The following is the method I'm using to call via POST the AddAttachment method:

 

string POSTAttachment(string url, string jsonContent,string ruta, string nombre)
{
   try
   {
      var aRespStr = "";

      using (System.Net.WebClient client = new System.Net.WebClient())
      {
         client.Headers["Content-type"] = "application/x-www-form-urlencoded";
         client.Encoding = System.Text.Encoding.UTF8;
         var collection = new System.Collections.Specialized.NameValueCollection();
         collection.Add("f", "json");
         collection.Add("attachment", jsonContent);
         client.UploadFile(ruta, nombre);
         var response = client.UploadValues(url, "POST", collection);
         MemoryStream stream = new MemoryStream(response);
         StreamReader reader = new StreamReader(stream);
         aRespStr = reader.ReadToEnd();
      }

      return aRespStr;
   }
   catch (Exception ex)
   {
      return "";
   }
}

 

I'm calling the method in this way:


string jsonAttachment = "[{\"attachment\":\"img.jpg\"}]";
string resultadoAttachment = POSTAttachment("http://crdes09:6080/arcgis/rest/services/Servicio_Prueba/Mapa_Control_Vectores_Movil_/FeatureServer/16/962/addAttachment", jsonAttachment, "C:\\img1.jpg", "img1.jpg");

 

 

My doubt is about how to embed the file into the call to the AddAttachment, is something i'm missing to do to solve this message about casting?

 

I'll really appreciated any help.

Outcomes