0

I am wanting to create a download link which will download a folder full of images based on the link directory, or create multiple links which will toggle a download of single images.

I have found the code below, however, this only seems to work on images which are local, not from a external url which is where I store my images, does anyone have any suggestions?

<a href="/path/to/image" download>
    <img src="/path/to/image" />
</a>
  • You cannot download a folder with a single link. You need to create a zip-archive and send that to the client. Or create a download link per file, but that would mean that the user has to click x links manually. – VDWWD Jan 17 '18 at 14:06
  • Thanks, I can have buttons for each images then, to download each one individually on each click, do you know of a way to do this with external link paths @VDWWD –  Jan 17 '18 at 14:26
  • Browsers will not ask to download images by default.. – Brainfeeder Jan 17 '18 at 14:36

2 Answers2

1

You can do this with Directory.GetFiles and bind the files directly to a Repeater Control.

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>

        <asp:HyperLink ID="HyperLink1" runat="server" Target="_blank"
            NavigateUrl='<%# "/images/" + System.IO.Path.GetFileName(Container.DataItem.ToString()) %>'>
            <%# System.IO.Path.GetFileName(Container.DataItem.ToString()) %>
        </asp:HyperLink>

        <br />

    </ItemTemplate>
</asp:Repeater>

Then in code behind you bind the files to the Repeater

Repeater1.DataSource = Directory.GetFiles(Server.MapPath("~/images/"), "*.jpg");
Repeater1.DataBind();

See this link for more complex filter option for getting all the image files from a folder: C#:Getting all image files in folder

VDWWD
  • 35,079
  • 22
  • 62
  • 79
0
<a download="yourfilename.jpg" href="http://www.example.com/path/to/image.jpg" title="Image title">
    <img alt="Image title" src="http://www.example.com/path/to/image.jpg">
</a>

This download attribute might not be supported by all modern browsers yet.. check the tables. Safari at this point will not work for example.

Alternatively you can opt to cook your own download script. Check This answer for an example with PHP.

Brainfeeder
  • 2,604
  • 2
  • 19
  • 37