How to download all the attachment from page
Asked by Shaksham Singh:

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 ?


Answers:
author: Marty
body: Hi Shaksham,

are you trying to download attachments from a Confluence Page?

comments: author: Shaksham Singh
body: 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..?


author: Marty
body: It depends on which version of Confluence you are using.

If you are using Confluence Cloud, the REST API docs are here:

<https://developer.atlassian.com/cloud/confluence/rest/>

Specifically, the attachment methods are here:

* [https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-get](https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-get?)
* <https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-post>
* <https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-put>
* <https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-attachmentId-put>
* <https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-attachmentId-data-post>

If you are using Confluence Server, the REST API docs are here:

<https://docs.atlassian.com/atlassian-confluence/REST/6.6.0>

You'll need to confirm the actual version of Confluence Server you're running and change that URL accordingly.

Specifically, the attachment methods are here: <https://docs.atlassian.com/atlassian-confluence/REST/6.6.0/#content/{id}/child/attachment>


author: Adapty_Solutions
body: [@Marty](/t5/user/viewprofilepage/user-id/880798) 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.


author: Marty
body: 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/](https://developer.atlassian.com/cloud/confluence/basic-auth-for-rest-apis/?)


author: Shaksham Singh
body: Hello [@Marty](/t5/user/viewprofilepage/user-id/880798) 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.


author: Shaksham Singh
body: 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](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


author: Adapty_Solutions
body: 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


Tags: developer