Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to download all the attachment from page

Shaksham Singh February 9, 2019

I am creating a C# application where i wish to download all the attachments from my page.

Also i wish to upload 2 documents at the same time from my C# app. 

Can anybody share me some code ?

2 answers

1 accepted

1 vote
Answer accepted
Marty
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 9, 2019

Hi Shaksham,

are you trying to download attachments from a Confluence Page?  

Shaksham Singh February 9, 2019

Yes i have uploaded some .txt, .png files on my confluence page and i am trying to download all attachments via C# application. Pls help me out. Which api i should call..?

Like Marty likes this
Marty
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 10, 2019
Like Shaksham Singh likes this
Adapty_Solutions February 11, 2019

@Marty Thanks for the answer.

Here is what i have tried till now but i am getting exception 302:

const string apiUrl = "https://connectpoc.atlassian.net/wiki/rest/api/content/98336/child/attachment";
var url = string.Empty;
var pathToSave = string.Empty;
Task.Run(async () =>
{
var tracker = Stopwatch.StartNew();

using (var client = new HttpClient())
{
var byteArray = Encoding.ASCII.GetBytes("myusername:password");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

var httpResponseMessageObj = await client.GetAsync(apiUrl);
httpResponseMessageObj.EnsureSuccessStatusCode();

HttpContent content = httpResponseMessageObj.Content;

var data = await content
.ReadAsStringAsync(); // return JSON Only

var result = JsonConvert.DeserializeObject<RootObject>(data);
var baseurl = result._links.@base.ToString();
// Console.WriteLine(result);
// var base=result.results[0]._links.
var download = result.results[0]._links.download.ToString();

url = baseurl + download;
var filename = result.results[0].title;
pathToSave = @"C:\Reports\" + filename;

var index = url.IndexOf("?", StringComparison.Ordinal);

if (index > 0)
url = url.Substring(0,
index);

var clients = new WebClient
{
Credentials = new NetworkCredential("username", "password")
};
clients.DownloadData(url);  // i am getting exception here "The remote server returned an error: (302) Moved Temporarily."


return data;
}

}).Wait();

 Can anybody tell me how to solve this issue i tried every code but still its not working.

Like Marty likes this
Marty
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 11, 2019

Hi there,

a 302 is a redirect, and my hunch is that it is not authenticating properly.

I noticed that you are base64 encoding that username:password string and putting it in the Basic Auth header, which is good.  Thanks for not posting the real username / password, it's so easy to do.

I just wanted to confirm that the password you're using here is an API token and not the password you use when you login to your instance?

https://developer.atlassian.com/cloud/confluence/basic-auth-for-rest-apis/ 

Like Shaksham Singh likes this
Shaksham Singh February 12, 2019

Hello @Marty Thank you so much your suggestion helped a lot. I am done with downloading attachment and very soon i will be creating a repository so that it may help other developers when needed.

Shaksham Singh February 12, 2019

Hi , Can you suggest me why i am getting bad request when creating attachment from my C# app. (i am using this https://developer.atlassian.com/cloud/confluence/rest#api-content-id-child-attachment-post)

here is my code :

const string apiUrl = "https://connectpoc.atlassian.net/wiki/rest/";

const string fullFileName = "sayHi.txt";
const string filepath = @"E:\Data\sayHi.txt";

var client = new RestClient(apiUrl)
{
Authenticator = new HttpBasicAuthenticator(
"gaurav.saxena@adapty.com",
"adapty123$")
};

var request = new RestRequest("api/content/98336/child/attachment",
Method.POST) {AlwaysMultipartFormData = true};

request.AddHeader("X-Atlassian-Token","nocheck");

// request.AddHeader("Accept",
// "application/json");

request.AddHeader("Content-Type","multipart/form-data");

request.AddParameter("minorEdit","true");
request.AddParameter("comment","Only for testing purpose");

//what should i do here to do add multiple files ??
request.AddParameter(Path.GetFileNameWithoutExtension(fullFileName),
filepath);

var response = client.Execute(request); //here i am getting bad request
var content = response.Content;

 I am using this https://github.com/restsharp/RestSharp 

Postman returns everything perfect , its seems something wrong with my code.

Am i making mistake in providing parameters ?  Pls suggest

0 votes
Adapty_Solutions February 13, 2019

Hi all, I got the solution for my above query 

the only issue with the above code was  replace below line 

request.AddParameter(Path.GetFileNameWithoutExtension(fullFileName),
filepath);

 with

request.AddFile("file", entity.FilePath); 

 

it must be AddFile when you posting a file i was using AddParameter.

AddFile Worked for me.

Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events