html download属性重定向到url而不是下载 [英] html download attribute redirects to url instead of downloading
问题描述
Noob Web开发人员在这里.
Noob webdeveloper here.
我正在尝试从单击中的URL下载图像.但是,当我使用图片网址作为自己的href时,它只会重定向到该网址,而不是下载.当然,我正在使用download属性.我尝试了自己的代码,还尝试了其他人的多个代码块.但是所有这些都只是重定向.我正在使用谷歌浏览器.
I'm trying to download an image from an url on click. But when I use the image url as my href it just redirects to that url instead of downloading. Of course I am using the download attribute. I have tried my own code and also mulitple code blocks from other people. But all of them just redirect. I am using google chrome.
我的代码:
<a href = "https://autoooo.nl/wp-content/uploads/2018/12/F5144B9C-2B27-4070-828E-2361EBD702EF-400x400.jpeg" download="car" id="downloadQRCodeButtonHref">
<p>download</p>
</a>
我从其他人使用的代码 1(接受的答案):
Code I used from someone else 1(accepted answer):
<a download="custom-filename.jpg" href="/path/to/image" title="ImageName">
<img alt="ImageName" src="/path/to/image">
</a>
我从其他人使用的代码 2:
<p> Click the image ! You can download! </p>
<?php
$image = basename("http://localhost/sc/img/logo.png"); // you can here put the image path dynamically
//echo $image;
?>
<a download="<?php echo $image; ?>" href="http://localhost/sc/img/logo.png" title="Logo title">
<img alt="logo" src="http://localhost/sc/img/logo.png">
</a>
一些帮助将不胜感激.我可能只是个大傻瓜而忽略了一些非常明显的事情.
Some help will be appreciated. I might just be a big dum dum and overlook something extremely obvious.
推荐答案
问题是因为您使用的是跨域URL.从 download
属性的文档中:
The problem is because you're using a cross-domain URL. From the documentation for the download
attribute:
download
仅适用于同源URL或blob:
和data:
方案.
download
only works for same-origin URLs, or theblob:
anddata:
schemes.
要解决此问题,您需要将映像托管在与父站点相同的域中.
To fix this you need to host the image on the same domain as the parent site.
这篇关于html download属性重定向到url而不是下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!