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.

  • 1
    Can you add some code as to how you formed this url like REST API ? – Gautam Sheth Feb 3 '17 at 9:51
  • Sorry missing that part but your answer already what i am looking for. thanks! – TyForHelpDude Feb 3 '17 at 9:59
  • What is "ofc" (in this context)? Original female character? Of course? Something else? – Peter Mortensen Feb 3 '17 at 17:33
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;
}
| improve this answer | |
  • Can you check the post please, AttachmentFiles contains no information about files in response, what i am doing wrong here ? – TyForHelpDude Feb 3 '17 at 10:16
  • 1
    did you append $expand=AttachmentFiles in the query ? – Gautam Sheth Feb 3 '17 at 10:18
  • Yeah its ok now :) – TyForHelpDude Feb 3 '17 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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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