Upload item

753
2
09-06-2013 06:52 AM
Labels (1)
RichardWatson
Frequent Contributor
I want to upload items to ArcGIS Online, e.g. tile packages and feature data.

It appears that the ArcGISWebClient class is designed to be used to communicate with AGOL but does not appear to have any methods to upload items.  I know that I can use the underlying Microsoft classes to do all of this myself but it seems like a lot of plumbing to me.

Is there some built-in way to do this?
0 Kudos
2 Replies
AnttiKajanus1
Occasional Contributor III
Hi,

Unfortunately there isn't Task's for doing those in the SDK but you can use ArcGISWebClient, thou it is quite a messy. I think I will write some extension Tasks to upload and download stuff to/from ArcGIS Online, I already working with other REST based Tasks so might to implement this as well.

If you want to use ArcGISWebClient here is a demo about it, not a pretty one but seems to work (tested with .mpk / .tpk files) thou there is a small delay when you can see added item in ArcGIS Online:

namespace ArcGISWpfApplication11
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Web.Script.Serialization;
    using System.Windows;


    using ESRI.ArcGIS.Client;


    using Microsoft.Win32;


    public partial class MainWindow
    {
        private readonly JavaScriptSerializer serializer = new JavaScriptSerializer();


        private string AddItemToUrlTemplate = @"http://www.arcgis.com/sharing/rest/content/users/{0}/addItem";


        private string AddPartUrlTemplate = @"http://www.arcgis.com/sharing/rest/content/users/{0}/items/{1}/addPart";


        private string CommitAddItemUrlTemplate =
            @"http://www.arcgis.com/sharing/rest/content/users/{0}/items/{1}/commit";


        private string _fileName = string.Empty;


        private string _filePath = string.Empty;


        private string _itemId = string.Empty;


        private Dictionary<string, string> _parameters;


        private string _token = "";


        private string password = "";


        private string userName = "kajanus_dev";


        public MainWindow()
        {
            // License setting and ArcGIS Runtime initialization is done in Application.xaml.cs.


            InitializeComponent();


            IdentityManager.Current.GenerateCredentialAsync(
                "http://www.arcgis.com/sharing/rest/",
                userName,
                // here should be your username 
                password,
                // here should be your password 
                (credential, ex) =>
                    {
                        if (ex != null)
                        {
                            MessageBox.Show(ex.ToString());
                            return;
                        }


                        var dlg = new OpenFileDialog();


                        //Open the Pop-Up Window to select the file 
                        if (dlg.ShowDialog() == true)
                        {
                            try
                            {
                                _token = credential.Token;
                                _filePath = dlg.FileName;
                                _fileName = dlg.SafeFileName;


                                _parameters = new Dictionary<string, string>
                                    {
                                        { "f", "pjson" },
                                        { "token", _token },
                                        { "multipart", "true" },
                                        { "filename", _fileName },
                                    };


                                var initializationClient = new ArcGISWebClient();
                                initializationClient.DownloadStringCompleted +=
                                    InitializationClientOnDownloadStringCompleted;
                                initializationClient.DownloadStringAsync(
                                    new Uri(string.Format(AddItemToUrlTemplate, userName)), _parameters);
                            }
                            catch (Exception exception)
                            {
                                return;
                            }
                        }
                    });
        }


        private void InitializationClientOnDownloadStringCompleted(
            object sender, ArcGISWebClient.DownloadStringCompletedEventArgs downloadStringCompletedEventArgs)
        {
            var results =
                serializer.DeserializeObject(downloadStringCompletedEventArgs.Result) as IDictionary<string, object>;


            if (results.ContainsKey("id"))
            {
                _itemId = results["id"].ToString();
            }


            var uploadClient = new ArcGISWebClient();


            _parameters = new Dictionary<string, string> { { "f", "json" }, { "token", _token }, { "partNum", "1" } };


            //// Upload the content
            //// Create url
            string addItemPartfullUrl = string.Format(AddPartUrlTemplate, userName, _itemId);
            uploadClient.PostMultipartCompleted += UploadClientOnPostMultipartCompleted;


            var contentStreams = new List<ArcGISWebClient.StreamContent>
                {
                    new ArcGISWebClient.StreamContent
                        {
                            ContentType = "File",
                            Filename = _fileName,
                            Name = "file",
                            Stream = File.OpenRead(_filePath)
                        }
                };


            uploadClient.PostMultipartAsync(new Uri(addItemPartfullUrl), _parameters, contentStreams);
        }


        private void UploadClientOnPostMultipartCompleted(
            object sender, ArcGISWebClient.PostMultipartCompletedEventArgs postMultipartCompletedEventArgs)
        {
            string streamText = new StreamReader(postMultipartCompletedEventArgs.Result).ReadToEnd();
            var addItemResult = serializer.DeserializeObject(streamText) as IDictionary<string, object>;


            string success = string.Empty;
            if (addItemResult.ContainsKey("success"))
            {
                success = addItemResult["success"].ToString();
            }


            // Commit
            var commitClient = new ArcGISWebClient();


            _parameters = new Dictionary<string, string> { { "f", "json" }, { "token", _token }, };


            if (success.ToLowerInvariant() == "true")
            {
                // Create url for commit
                string fullCommitUrl = string.Format(CommitAddItemUrlTemplate, userName, _itemId);


                commitClient.DownloadStringCompleted += CommitClientOnDownloadStringCompleted;
                commitClient.DownloadStringAsync(new Uri(fullCommitUrl), _parameters, ArcGISWebClient.HttpMethods.Post);
            }
        }


        private void CommitClientOnDownloadStringCompleted(
            object sender, ArcGISWebClient.DownloadStringCompletedEventArgs downloadStringCompletedEventArgs)
        {
            MessageBox.Show(downloadStringCompletedEventArgs.Result);
        }
    }
}



Edit: And please note that this example doesn't take advantage on multipart post capability.
0 Kudos
RichardWatson
Frequent Contributor
Thanks for the reply.  It was helpful.

I wish that ESRI provided some samples which show the process of uploading files and, in particular, using the multipart load capability.
0 Kudos