下载文件夹中图像的链接,或下载直接图像的链接

时间:2018-01-17 13:59:05

标签: c# html asp.net html5 image-processing

我想创建一个下载链接,它会根据链接目录下载一个完整图像的文件夹,或者创建多个链接来切换单个图像的下载。

我已经找到了下面的代码,但是,这似乎只适用于本地的图像,而不是来自我存储图像的外部网址,是否有人有任何建议?

<a href="/path/to/image" download>
    <img src="/path/to/image" />
</a>

2 个答案:

答案 0 :(得分:1)

您可以使用Directory.GetFiles执行此操作,并将文件直接绑定到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>

然后在代码后面将文件绑定到Repeater

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

有关获取文件夹中所有图像文件的更复杂过滤器选项,请参阅此链接:C#:Getting all image files in folder

答案 1 :(得分: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>

所有现代浏览器可能都不支持此download属性.. check the tables。此时Safari不会起作用。

或者,您可以选择烹饪自己的下载脚本。查看This answer以获取PHP的示例。

相关问题