简体   繁体   English

如何下载保存在html DB中的图片

[英]How to download images saved on the DB in html

My code is basically this... If there was a functionality like "controls" for video tag, would be awesome, but there isn't.我的代码基本上是这样的……如果有视频标签的“控件”之类的功能,那就太棒了,但是没有。 I do not know to mae a button or icon, etc, to download the image.我不知道要使用按钮或图标等来下载图像。 I also tried <a href="" download> Download Link </a> , but it does not work because the image is not static on a file, but dynamically saved on the DB.我也试过<a href="" download> Download Link </a> ,但它不起作用,因为图像不是文件上的 static,而是动态保存在数据库上。 I just need a download button/icon to download the actual image being displayed.我只需要一个下载按钮/图标来下载正在显示的实际图像。

          <!-- album starts -->
             
          <div class="full-img" id="fullImgBox">

              <img src="images/img1.jpg" id="fullImg" controls class="media">
              <span class="material-icons" onclick="closeFullImg()">close</span>
          </div>
          <div class="img-gallery">
          <?php if(count($posts) !== 0) { ?>

              <?php foreach($posts as $post) { 
                if( $post['post_type'] == "image"){
              ?>
                <img src="<?= POSTS_SAVED_TO.$post['uploaded_file_name'] ?>" onclick="openFullImg(this.src)"/>
              <?php
                }
              } 
           } else { ?> 
          <p class="alert alert-info">No Posts are made yet !!.</p>
          <?php } ?>
          </div>

          <script>

              var fullImgBox = document.getElementById("fullImgBox");
              var fullImg = document.getElementById("fullImg");

              function openFullImg(pic) {
                  fullImgBox.style.display = "flex";
                  fullImg.src = pic;
              }

              function closeFullImg() {
                  fullImgBox.style.display = "none";
              }

          </script>

As long as your image is on the same domain as your script you can use the download attribute on an anchor:只要您的图像与您的脚本位于同一域中,您就可以在锚点上使用download属性:

<a href="<?= POSTS_SAVED_TO.$post['uploaded_file_name'] ?>" download="myfile">
    <img src="<?= POSTS_SAVED_TO.$post['uploaded_file_name'] ?>" />
</a>

The myfile property of the download attribute will be the name of the downloaded file, and the extension will be set automatically.下载属性的myfile属性将是下载文件的名称,扩展名将自动设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM