gpt4 book ai didi

javascript - 当来自本地主机的图像时,打印图像源本身不起作用

转载 作者:行者123 更新时间:2023-12-02 21:18:36 24 4
gpt4 key购买 nike

我尝试在用户点击图像时打印图像。

当img来自互联网时它可以工作,但当它来 self 自己的电脑(本地主机)时它不起作用。但为什么呢?

我尝试了很多脚本,但都给出了相同的结果。

提前致谢

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="tab"><img style="width:600px; hieght:600px;" src="https://scontent.fgza6-1.fna.fbcdn.net/v/t1.15752-9/90511757_144802246885621_238915900961456128_n.jpg?_nc_cat=111&_nc_sid=b96e70&_nc_ohc=r3aJA4RgmrIAX9uj7MO&_nc_ht=scontent.fgza6-1.fna&oh=bc1a6cdf63922303eb725277d019cffa&oe=5EA69107" alt="Bald Eagle" />
</div>


<div id="tab2"><img style="width:600px; hieght:600px;" src="http://localhost:8000/storage/images/VW5wb8YGHetqZy8BKcnQuCVGcJwqXgakKViyeuoV.jpeg" alt="Bald Eagle" />
</div>

<p>
<input type="button" value="Print img" onclick="myApp.print()" /> <!-- it's work -->
<input type="button" value="Print img local" onclick="myApp2.printLocal()" /> <!-- it not work !! -->
</p>
</body>
<script>
var myApp = new function () {
this.print = function () {
var tab = document.getElementById('tab');
var win = window.open('', '', 'height=508,width=580');
win.document.write(tab.outerHTML);
win.document.close();
win.print();
}
}

var myApp2 = new function () {
this.printLocal = function () {
var tab = document.getElementById('tab2');
var win = window.open('', '', 'height=508,width=580');
win.document.write(tab.outerHTML);
win.document.close();
win.print();
}
}
</script>
</html>

最佳答案

我已经在本地主机上尝试过你的代码,它工作完美,并且我确实给出了src="/storage/images/VW5wb8YGHetqZy8BKcnQuCVGcJwqXgakKViyeuoV.jpeg" 而不是 src="http://localhost:8000/storage/images/VW5wb8YGHetqZy8BKcnQuCVGcJwqXgakKViyeuoV.jpeg"

试试你的运气

从上传的文件中打印

HTML

<input type="file" accept="img/*" onchange="fileChanged(event)" /> 

JS

function fileChanged(e){
let file=e.target.files[0]; document.querySelector('#tab2>img').src=window.URL.createObjectURL(file);
}

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="tab"><img style="width:100px; height:100px;" src="https://scontent.fgza6-1.fna.fbcdn.net/v/t1.15752-9/90511757_144802246885621_238915900961456128_n.jpg?_nc_cat=111&_nc_sid=b96e70&_nc_ohc=r3aJA4RgmrIAX9uj7MO&_nc_ht=scontent.fgza6-1.fna&oh=bc1a6cdf63922303eb725277d019cffa&oe=5EA69107" alt="Bald Eagle" />
</div>


<div id="tab2"><img style="width:100px; height:100px;" src="" />
</div>

<p>
<input type="button" value="Print img" onclick="myApp.print()" /> <!-- it's work -->
<input type="file" accept="img/*" onchange="fileChanged(event)" />
<input type="button" value="Print img local" onclick="myApp2.printLocal()" /> <!-- it not work !! -->
</p>
</body>
<script>
var myApp = new function () {
this.print = function () {
var tab = document.querySelector('#tab>img');
var win = window.open('', '', 'height=508,width=580');
win.document.write(tab.outerHTML);
win.document.close();
win.print();
}
}
function fileChanged(e){
let file=e.target.files[0]; document.querySelector('#tab2>img').src=window.URL.createObjectURL(file);
}
var myApp2 = new function () {
this.printLocal = function () {
var tab = document.querySelector('#tab2>img');
var win = window.open('', '', 'height=508,width=580');
win.document.write(tab.outerHTML);
win.document.close();
win.print();
}
}
</script>
</html>

关于javascript - 当来自本地主机的图像时,打印图像源本身不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60907793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com