Edit feature attachments

View inQMLC++
View on GitHub
Sample viewer app

Add, delete, and download attachments for features from a service.

screenshot

Use case

Attachments provide a flexible way to manage additional information that is related to your features. Attachments allow you to add files to individual features, including: PDFs, text documents, or any other type of file. For example, if you have a feature representing a building, you could use attachments to add multiple photographs of the building taken from several angles, along with PDF files containing the building's deed and tax information.

How to use the sample

Tap a feature on the map to open a callout displaying the number of attachments. Tap on the info button to view/edit the attachments. Select an entry from the list to download and view the attachment in the gallery. Tap on the floating action button '+' or '-' too add or remove an attachment.

How it works

  1. Create a ServiceFeatureTable from a URL.
  2. Create a FeatureLayer object from the service feature table.
  3. Select features from the feature layer with selectFeatures.
  4. To fetch the feature's attachments, cast to an ArcGISFeature and useArcGISFeature::attachments().
  5. To add an attachment to the selected ArcGISFeature, create an attachment and use ArcGISFeature::attachments()::addAttachment().
  6. To delete an attachment from the selected ArcGISFeature, use the ArcGISFeature.deleteAttachment().
  7. By default, edits are automatically applied to the service and applyEdits does not need to be called.

Additional information

Attachments can only be added to and accessed on service feature tables when their hasAttachments property is true.

Relevant API

  • ApplyEdits
  • AttachmentListModel
  • DeleteAttachment
  • FeatureLayer
  • FetchAttachments
  • FetchData
  • ServiceFeatureTable
  • UpdateFeature

Tags

Edit and Manage Data, image, picture, JPEG, PNG, PDF, TXT

Sample Code

EditFeatureAttachments.qmlEditFeatureAttachments.cppEditFeatureAttachments.h
                                                                                                                                                                                                                               
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// [WriteFile Name=EditFeatureAttachments, Category=EditData]
// [Legal]
// Copyright 2016 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

import QtQuick 2.6
import QtQuick.Controls 2.2
import Qt.labs.platform 1.0
import QtGraphicalEffects 1.0
import Esri.Samples 1.0
import Esri.ArcGISRuntime.Toolkit 100.12

EditFeatureAttachmentsSample {
    id: editAttachmentsSample

    readonly property var featAttributes: ["Destroyed", "Major", "Minor", "Affected", "Inaccessible"]

    // add a mapView component
    MapView {
        id: mapView
        anchors.fill: parent
        objectName: "mapView"

        Callout {
            id: callout
            borderColor: "lightgrey"
            borderWidth: 1
            calloutData: mapView.calloutData
            leaderPosition: leaderPositionEnum.Automatic
            onAccessoryButtonClicked: {
                attachmentWindow.visible = true;
            }
        }
    }

    onFeatureSelected: {
        // show the callout
        callout.showCallout();
    }

    onHideWindow: {
        // hide the callout
        if (callout.visible)
            callout.dismiss();
        attachmentWindow.visible = false;
    }

    // attachment window
    Rectangle {
        id: attachmentWindow
        anchors.centerIn: parent
        height: 200
        width: 250
        visible: false
        radius: 10
        color: "lightgrey"
        border.color: "darkgrey"
        opacity: 0.90
        clip: true

        // accept mouse events so they do not propogate down to the map
        MouseArea {
            anchors.fill: parent
            onClicked: mouse.accepted = true
            onWheel: wheel.accepted = true
        }

        Rectangle {
            id: titleText
            anchors {
                left: parent.left
                right: parent.right
                top: parent.top
            }
            height: 40
            color: "transparent"

            Text {
                anchors {
                    verticalCenter: parent.verticalCenter
                    left: parent.left
                    margins: 10
                }
                text: "Attachments"
                font {
                    bold: true
                    pixelSize: 20
                }
            }

            Row {
                anchors {
                    verticalCenter: parent.verticalCenter
                    right: parent.right
                    margins: 10
                }
                spacing: 15
                Text {
                    text: "+"
                    font {
                        bold: true
                        pixelSize: 40
                    }
                    color: "green"

                    // open a file dialog whenever the add button is clicked
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            fileDialog.open();
                        }
                    }
                }
                Text {
                    text: "-"
                    font {
                        bold: true
                        pixelSize: 40
                    }
                    color: "red"

                    // make sure an item is selected and if so, delete it from the service
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            if (attachmentsList.currentIndex !== -1)  {
                                // Call invokable C++ method to add an attachment to the model
                                editAttachmentsSample.deleteAttachment(attachmentsList.currentIndex);
                            }
                        }
                    }
                }
            }
        }

        ListView {
            id: attachmentsList
            anchors {
                left: parent.left
                right: parent.right
                top: titleText.bottom
                bottom: parent.bottom
                margins: 10
            }
            clip: true
            spacing: 5
            // set the model equal to the C++ attachment list model property
            model: editAttachmentsSample.attachmentModel
            // create the delegate to specify how the view is arranged
            delegate: Item {
                height: 45
                width: parent.width
                clip: true

                // show the attachment name
                Text {
                    id: label
                    anchors {
                        verticalCenter: parent.verticalCenter
                        left: parent.left
                        right: attachment.left
                    }
                    text: name
                    wrapMode: Text.WrapAnywhere
                    maximumLineCount: 1
                    elide: Text.ElideRight
                    font.pixelSize: 16
                }

                // show the attachment's URL if it is an image
                Image {
                    id: attachment
                    anchors {
                        verticalCenter: parent.verticalCenter
                        right: parent.right
                    }
                    width: 44
                    height: width
                    fillMode: Image.PreserveAspectFit
                    source: attachmentUrl
                }

                MouseArea {
                    id: itemMouseArea
                    anchors.fill: parent
                    onClicked: {
                        attachmentsList.currentIndex = index;
                    }
                }
            }

            highlightFollowsCurrentItem: true
            highlight: Rectangle {
                height: attachmentsList.currentItem.height
                color: "lightsteelblue"
            }
        }
    }

    // file dialog for selecting a file to add as an attachment
    FileDialog {
        id: fileDialog
        folder: {
            const locs = StandardPaths.standardLocations(StandardPaths.PicturesLocation)
            return locs.length > 0 ? locs.last() : "";
        }
        onAccepted: {
            // Call invokable C++ method to add an attachment to the model
            editAttachmentsSample.addAttachment(fileDialog.file, "application/octet-stream");
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.