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); } } }
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.