About attachments in flex viewer

6862
36
Jump to solution
04-21-2015 11:04 AM
EvelynHernandez
Occasional Contributor III

Hello,

I am developing an app that needs to add an attachment (pdf file) and save it in the arcgis db. Anyone knows how to do that?

My rest service has the attachment value in True, but idk why this code doesnt do it.

This is part of my code:

       myPropLayer.url = "myfeaturelayerurl";

myPropLayer.id = "NewEtaFeature";

  myPropLayer.addAttachment(1,byteArrayToProcess,"ElemetopPDF","pdf",new AsyncResponder(resultado,malresultado));

function resultado():void{

Alert.show("saved");

}

function malresultado():void{

Alert.show("not saved");

}

Mensaje editado por: Evelyn Hernandez. new Question about attachments

0 Kudos
36 Replies
NigelDsouza
Occasional Contributor

Hi Evelyn,

The featurelayer class in the Flex API does not provide any method for updating an attachment. However the REST API does support it, if you've noticed a "UpdateAttachment" method when you navigated to a feature in your rest service.

If you want to delete an uploaded attachment/s then all you need to do is call the "deleteAttachments" on the featureLayer. This method needs the object_id of the feature and the attachment id/ids(if more than one attachment exists).

If you do not know the attachment ids.

Then simply first call

queryAttachmentInfos(objectId:Number, responder:IResponder = null):AsyncToken method which necessarily requires an objectid to query.

and then call the  "deleteAttachments" method on the feature service.

Regards,

Nigel.

EvelynHernandez
Occasional Contributor III

Nigel, im trying now to get the SRC and convert it to a bytearray to make a preview imag.

Using the queryAttachmentInfos to get them. I dont understand how to use it well.

Later i will have to delete it a image too , so.. is there any example how to use both?

Thank you.

public function searchAndLoadPics():void{


  //my object id for getting the attachment
  var objActividad:int= DataActividades.selectedItem['OBJECTID'];
  myActividadLayer.queryAttachmentInfos(objActividad, responder:IResponder = null):AsyncToken;


  // load the pics on the canvas
  var imageAntes : Image = new Image();

  imageAntes.source = myByteArrayBefore;
  imageAntes.scaleContent = true;
  imageAntes.maintainAspectRatio =false;
  imageAntes.percentWidth = 150;
  imageAntes.percentHeight = 150;

  pbMODAntes1.addChild(imageAntes);


  var imageDespues : Image = new Image();
  //image.
  imageDespues.source = myByteArrayAfter;
  imageDespues.scaleContent = true;
  imageDespues.maintainAspectRatio =false;
  imageDespues.percentWidth = 150;
  imageDespues.percentHeight = 150;

  pbMODDespues1.addChild(imageDespues);
  }
0 Kudos
NigelDsouza
Occasional Contributor

Hi Evelyn,

I am not clear about your question but for deleting an attachment you can use the same example i gave sent

just instead of the featureLayer.addAttachments method you call the featureLayer.deleteAttachments method.

As far as querying an attachment is concerned you can use the

queryAttachmentinfos(objectid,new AsyncResponder(onResult,onFault));

function onResult(event:AttachmentEvent,token:object=null):void{

var arry:Array = event.attachmentInfos;

     (for example)

     var attachmentInfo:Attachmentinfo = arry[0];

     var imageUrl:String = attachmentInfo.url;

}

function onFault(event:FaultEvent,token:object=null):void{}

the onResult event of type AttachmentEvent will have an attachmentInfos array.. pick each attacment from the array an place it into a attachmentinfo object. attahment info has a property url which gives you the entire url of the attachment for you to download or view.

regards,

Nigel

0 Kudos
EvelynHernandez
Occasional Contributor III

Well im having a issue with the event:AttachmentEvent.

the debugger says

TypeError: Error #1034: Type Coercion failed: cannot convert []@128463f9 to com.esri.ags.events.AttachmentEvent.

My code is:

public function buscarCargarFotografias():void{

  var arrayInfos:ArrayCollection = new ArrayCollection;
  //buscarOBJIDActividad(numAct:int, numSoli:int)
  var objActividad:int= DataActividades.selectedItem['OBJECTID'];
  myActividadLayer.queryAttachmentInfos(objActividad,new AsyncResponder(onResult,onFault));

  function onResult(event:AttachmentEvent, token:Object=null):void{


  var arry:Array = event.attachmentInfos;

  var attachmentInfo:AttachmentInfo= arry[0];
  var imageUrlAntes:String = String(attachmentInfo.url);

  var attachmentInfo2:AttachmentInfo= arry[1];
  var imageUrlDespues:String = attachmentInfo2.url;



  var imageAntes : Image = new Image();

  imageAntes.source = imageUrlAntes;
  imageAntes.scaleContent = true;
  imageAntes.maintainAspectRatio =false;
  imageAntes.percentWidth = 150;
  imageAntes.percentHeight = 150;

  pbMODAntes1.addChild(imageAntes);


  var imageDespues : Image = new Image();

  imageDespues.source = imageUrlDespues;
  imageDespues.scaleContent = true;
  imageDespues.maintainAspectRatio =false;
  imageDespues.percentWidth = 150;
  imageDespues.percentHeight = 150;

  pbMODDespues1.addChild(imageDespues);


  }
0 Kudos
NigelDsouza
Occasional Contributor

Hi,

Oh If your getting this error then it means that the object that's being sent from the server isn't of  the same type as the attachmentEvent object type.

But the documentation says that this is the event when you use queryAttacmentInfos. Have a look at the attachmentinfos property here.

com.esri.ags.events.AttachmentEvent

Regards,

Nigel.

0 Kudos
EvelynHernandez
Occasional Contributor III

Yes i ckecked it already with the docs, but now idk what i can do , idk if i can parse the event ..

0 Kudos
NigelDsouza
Occasional Contributor

instead of attachmentEvent try writing object. Like event:Object it should work.

0 Kudos
EvelynHernandez
Occasional Contributor III

Nigel, I did and i have another kind of error,

TypeError: Error #1009: Cannot access a property or method of a null object reference.

The widget that im doing has to get some information and the attachment related to that info.

Im using this layer: Layer: ARCGIS.DBO.MANTENIMIENTO_CONTROL_PODAS (ID: 4)

Example: Here i have 2 attachments: Attachment Infos (Feature ID: 2807)

And i need to see on a form the preview image saved in the service (with other info that i already have done through code).

After that, the user can modify or eliminate the info related (could be just the info, just the attachment or both).

So, for that now i need to see how to get the attachments ID for modifying or deleting the images, but how u told me before i cannot modify through code (just service allows it) my option is deleting the attachment and create them again (for modifying).

Now with the code it is atleast passing the onResult function, but now how i get the url images and the attachment IDs related, it isnt just a pic, can be max 2).

My code is getting like this:

public function loadingPictures():void{  

  var arrayInfos:ArrayCollection = new ArrayCollection;  
  //buscarOBJIDActividad(numAct:int, numSoli:int)  
  var objActividad:int= DataActividades.selectedItem['OBJECTID'];  
  myActividadLayer.queryAttachmentInfos(objActividad,new AsyncResponder(onResult,onFault));  

  function onResult(event:Object, token:Object=null):void{  


  var arry:Array = event.attachmentInfos;  



  var attachmentInfo:AttachmentInfo= arry[0];  
  var imageUrlAntes:String = String(attachmentInfo.url);  

  var attachmentInfo2:AttachmentInfo= arry[1];  
  var imageUrlDespues:String = attachmentInfo2.url;  



  var imageAntes : Image = new Image();  

  imageAntes.source = imageUrlAntes;  
  imageAntes.scaleContent = true;  
  imageAntes.maintainAspectRatio =false;  
  imageAntes.percentWidth = 150;  
  imageAntes.percentHeight = 150;  

  pbMODAntes1.addChild(imageAntes);  


  var imageDespues : Image = new Image();  

  imageDespues.source = imageUrlDespues;  
  imageDespues.scaleContent = true;  
  imageDespues.maintainAspectRatio =false;  
  imageDespues.percentWidth = 150;  
  imageDespues.percentHeight = 150;  

  pbMODDespues1.addChild(imageDespues);  


  }  
  function onFault(event:Object,token:Object=null):void{
  Alert.show("It is not doing anythingAnd i ");
  }

  }

Thanks for all ur help as always

0 Kudos
EvelynHernandez
Occasional Contributor III

Im still getting the same error, idk why

0 Kudos
NigelDsouza
Occasional Contributor

Hi evelyn,

Have a look at this example here Attachment Inspector—ArcGIS API for Flex | ArcGIS for Developers

This sample uses the queryAttachmentInfos method and its respective event. Try creating a sample and using it like this.

Ill try my best to send you a sample soon.

regards,

Nigel