gpt4 book ai didi

javascript - 无法加载资源: the server responded with a status of 404 (Not Found) while loading image in img element

转载 作者:行者123 更新时间:2023-12-02 21:32:55 25 4
gpt4 key购买 nike

我想在从文件选择器对话框中选择图像时加载图像。

下面给出的代码,但它不会加载 img 标记中的图像

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<img id="upload-file-info" />
<form action="#">
<label class="btn btn-primary" for="my-file-selector">
<input id="my-file-selector" type="file" style="display:none" onchange="$('#upload-file-info').attr('src', this.files[0].name)">Click to select image
</label>
</form>
</div>

最佳答案

我相信您想要在 img 标记中预览图像。

您必须将imgsrc更改为图像的数据URL:

$('#upload-file-info').attr('src', window.URL.createObjectURL(this.files[0]))

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<img id="upload-file-info" />
<form action="#">
<label class="btn btn-primary" for="my-file-selector">
<input id="my-file-selector" type="file" style="display:none" onchange="$('#upload-file-info').attr('src', window.URL.createObjectURL(this.files[0]))">Click to select image
</label>
</form>
</div>

服务器给出 404 错误,因为 input 元素未将图像文件添加到服务器。您选择的图像仅保存在浏览器中。

关于javascript - 无法加载资源: the server responded with a status of 404 (Not Found) while loading image in img element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60574708/

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