Add PDF document (item) to organizational portal

850
1
11-04-2019 04:22 PM
LesleyBross1
New Contributor III

I'm trying to upload a PDF document to our organization's portal from an ArcGIS Pro add-in. I have been unable to find this functionality in the current API so am trying to use the REST API. When I run it, Pro churns away so I think it is uploading but I always receive a 502 Bad Gateway response.

Here is my current code:

var url = $"https://www.arcgis.com/sharing/rest/portals/self?f=json&token=" + strToken;
var response = new EsriHttpClient().Get(url);
var json = await response.Content.ReadAsStringAsync();
dynamic portalSelf = JObject.Parse(json);

var uploadUrl = "http://" + Convert.ToString(portalSelf.urlKey) +
".maps.arcgis.com/sharing/rest/content/users/" + owner + "/addItem";

byte[] fileBytes = System.IO.File.ReadAllBytes("C:\\Docs\\animas_AOI_prms\\test\\title_page.pdf");

string strTitle = "Testing 1 2 3";
using (var formData = new MultipartFormDataContent())
{
// Add the HttpContent objects to the form data
// <input type="text" name="f" />
formData.Add(new StringContent("f"), "json");
formData.Add(new StringContent("token"), strToken);
formData.Add(new StringContent("async"), "True");
formData.Add(new StringContent("type"), "PDF");
formData.Add(new StringContent("title"), strTitle);
formData.Add(new StringContent("tags"), "eBagis");
formData.Add(new StringContent("description"), "upload from BAGIS");
formData.Add(new ByteArrayContent(fileBytes, 0, fileBytes.Count()), "PDF Test", "title_page.pdf");

// Invoke the request to the server
// equivalent to pressing the submit button on
// a form with attributes (action="{url}" method="post")
response = await new EsriHttpClient().PostAsync(url, formData);
json = await response.Content.ReadAsStringAsync();
}

I found this Python script and think I am doing the same thing in C#, any ideas?

Tags (2)
0 Kudos
1 Reply
LesleyBross1
New Contributor III

I still haven't been able to get past the 502 Bad Gateway error but wanted to bump this issue back to the top and add my current code. I did find some mistakes in my previous submission. Hopefully someone has been able to get this function to work in the last year or so?

var myPortal = ArcGISPortalManager.Current.GetActivePortal();
string strToken = myPortal.GetToken();
var owner = myPortal.GetSignOnUsername();
var url = $"https://www.arcgis.com/sharing/rest/portals/self?f=json&token=" + strToken;
var response = new EsriHttpClient().Get(url);
var json = await response.Content.ReadAsStringAsync();
dynamic portalSelf = JObject.Parse(json);

var uploadUrl = "https://" + Convert.ToString(portalSelf.urlKey) +
".maps.arcgis.com/sharing/rest/content/users/" + owner + "/addItem";//convert filestream to byte array
byte[] fileBytes;
using (var fileStream = File.OpenRead("C:\\Docs\\animas_AOI_prms\\maps_publish\\title_page.pdf"))
{
var binaryReader = new BinaryReader(fileStream);
fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
}
var fileBinaryContent = new ByteArrayContent(fileBytes);

string strTitle = "Testing 1 2 3";

using (var formData = new MultipartFormDataContent())
{
// Add the HttpContent objects to the form dataformData.Add(new StringContent("json"), "f");
formData.Add(new StringContent(strToken), "token");
formData.Add(new StringContent("true"), "async");
formData.Add(new StringContent("PDF"), "type");
formData.Add(new StringContent(strTitle), "title");
formData.Add(new StringContent("eBagis"), "tags");
formData.Add(new StringContent("upload from BAGIS"), "description");
var multipartContent = new MultipartFormDataContent
{
{ fileBinaryContent, "file" }
};
formData.Add(multipartContent);

// Invoke the request to the server
// equivalent to pressing the submit button on
// a form with attributes (action="{url}" method="post")
response = await new EsriHttpClient().PostAsync(uploadUrl, formData);
json = await response.Content.ReadAsStringAsync();
}

0 Kudos