3

I have a custom list on sharepointonline, and I have attached a file (myfile.txt) to an item (itemid=5). How can I download this attachment?

I am able download the file with this button:

<a type="button" href={'https://mydomain/Lists/Announcements/Attachments/'+this.state.ItemId+'/myfile.txt'} className="btn btn-info" download>Download Attachment</a>

I am able to get ItemId with the REST API, but there is no name of attachment files. The only question here is: How do I get the attachment file names like other columns 'Title', 'Created'? I expect 'AttachmentName', but none ofc.

3
  • 1
    Can you add some code as to how you formed this url like REST API ? Feb 3, 2017 at 9:51
  • Sorry missing that part but your answer already what i am looking for. thanks! Feb 3, 2017 at 9:59
  • What is "ofc" (in this context)? Original female character? Of course? Something else? Feb 3, 2017 at 17:33

2 Answers 2

4

Your REST query should be as below:

/_api/Web/Lists/getByTitle('Announcements')/items?$select=Title,Created,Attachments,AttachmentFiles&$expand=AttachmentFiles

In the JSON results, you can get the file name as below:

for (var i = 0; i < data.d.results.length; i++) {    
    var item = data.d.results[i];
    var attachmentUrl = item.AttachmentFiles.results[0];
    var fileName = attachmentUrl.FileName;
}
3
  • Can you check the post please, AttachmentFiles contains no information about files in response, what i am doing wrong here ? Feb 3, 2017 at 10:16
  • 1
    did you append $expand=AttachmentFiles in the query ? Feb 3, 2017 at 10:18
  • Yeah its ok now :) Feb 3, 2017 at 10:22
3

You need to either use /AttachmentFiles like this

{weburl}/_api/web/lists/getByTItle('Announcements')/Items(5)/AttachmentFiles

or expand it like this

{weburl}/_api/web/lists/getByTItle('Announcements')/Items(5)?$expand=AttachmentFiles

The / method only works on single items, where expand works with your OData query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.