Uploading attachments
The upload operation is very similar to the download operation, except that you need to have attachment objects created and added to the FeatureSource via AttachmentManager, and set the FeatureAttachmentSyncAgent.SynchronizationDirection property to UploadOnly. The following steps illustrate how this can be accomplished.
This topic only focuses on uploading attachments To learn more about downloading attachments, see Synchronizing attachments.
Steps:
- Create a FeatureAttachmentSyncAgent object and set its SynchronizationDirection to UploadOnly.
MobileServiceConnection con = new MobileServiceConnection(); con.Url = @"http://NAMEOFYOURSERVER/ArcGIS/services/NAMEOFYOURSERVICE/MapServer/MobileServer"; FeatureSource featureSource = mobileCache.FeatureSources[0] as FeatureSource; FeatureAttachmentSyncAgent featureAttachmentSyncAgent = new FeatureAttachmentSyncAgent(featureSource, con); featureAttachmentSyncAgent.SynchronizationDirection = SyncDirection.UploadOnly;
- Create some attachment objects and add them to the designated feature using AttachmentManager.
//Create two attachment objects and add them to the feature 615. Attachment attachment1 = new Attachment(featureSource, 615, "WaterMeter1"); Attachment attachment2 = new Attachment(featureSource, 615, "WaterMeter2"); AttachmentManager manager = featureSource.AttachmentManager; if(manager.HasAttachments && manager.AllowEdits) { manager.AddAttachment(attachment1, @"C:\pics\Attachments\WaterMeter2011.jpg", FileOperation.CopyFile); manager.AddAttachment(attachment2, @"C:\pics\Attachments\WaterMeter2012.jpg", FileOperation.CopyFile); }
- Optionally, select a subset of attachments to upload. If this property is not set, all attachments of the FeatureSource are uploaded.
//Note that only attachment2 is uploaded, attachment1 stays in local storage. featureAttachmentSyncAgent.AttachmentsToUpload = new List<Attachment> { attachment2 };
- Upload the selected attachments.
featureAttachmentSyncAgent.Synchronize();
Note:
A FeatureAttachmentSyncResults object is returned when the Synchronize operation completes. To learn more about FeatureAttachmentSyncResults, see Understanding attachment sync results.
1/7/2015