如何以Django表格上传后显示图像

发布于 2025-02-10 23:37:45 字数 113 浏览 3 评论 0原文

我有一个image的模型,我希望当用户上传模型的图像字段时,图像应以表单显示,以便用户可以确认上传的映像,但我找不到任何容易完成此任务的解决方案。 请告诉我什么是实施此功能的好方法。

I have a model of image and I want that when a user uploads the image field of the model the image should be shown in the form so that user can confirm the uploaded image but I didn't find any easy solution to do this task.
Please show me what is the good and easy way to implement this functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2025-02-17 23:37:45

好吧,这可以通过几行JavaScript来实现。

html

 <input type="file" accept="image" id="img-input"/>
 <img id="display-img"/>

javascript

const imgInput = document.querySelector("#img-input")
const displayImg = document.querySelector("#display-img")
displayImg.src = URL.createObjectURL(imgInput.files[0])

或更实际的方法是在IMG输入 - &gt; 上添加事件列表

const imgInput = document.querySelector("#img-input")
const displayImg = document.querySelector("#display-img")
imgInput.addEventListener('change',(event)=>{
   const imgObject = event.target.files[0]
   displayImg.src = URL.createObjectURL(imgObject)})

,还检查此代码段。

const imgInput = document.querySelector("#img-input")
const displayImg = document.querySelector("#display-img")
imgInput.addEventListener('change',
(event)=>{
   const imgObject = event.target.files[0]
   displayImg.src = URL.createObjectURL(imgObject)})
<input type="file" accept="image" id="img-input"/>
 <img id="display-img"/>

Well this can be achieved with a very few lines of javascript.

HTML

 <input type="file" accept="image" id="img-input"/>
 <img id="display-img"/>

Javascript

const imgInput = document.querySelector("#img-input")
const displayImg = document.querySelector("#display-img")
displayImg.src = URL.createObjectURL(imgInput.files[0])

Or more practical way is to add event listner on img input ->

const imgInput = document.querySelector("#img-input")
const displayImg = document.querySelector("#display-img")
imgInput.addEventListener('change',(event)=>{
   const imgObject = event.target.files[0]
   displayImg.src = URL.createObjectURL(imgObject)})

And also check this code snippet.

const imgInput = document.querySelector("#img-input")
const displayImg = document.querySelector("#display-img")
imgInput.addEventListener('change',
(event)=>{
   const imgObject = event.target.files[0]
   displayImg.src = URL.createObjectURL(imgObject)})
<input type="file" accept="image" id="img-input"/>
 <img id="display-img"/>

驱逐舰岛风号 2025-02-17 23:37:45

CSS:

#display-image{
  width: 400px;
  height: 225px;
  border: 1px solid black;
  background-position: center;
  background-size: cover;
}

HTML:

<input type="file" id="image-input" accept="image/jpeg, image/png, image/jpg">

<div id="display-image"></div>

JS:

const image_input = document.querySelector("#image-input");
image_input.addEventListener("change", function() {
  const reader = new FileReader();
  reader.addEventListener("load", () => {
    const uploaded_image = reader.result;
    document.querySelector("#display-image").style.backgroundImage = `url(${uploaded_image})`;
  });
  reader.readAsDataURL(this.files[0]);
});

当输入图像字段更改时,负载事件就会发射。所选图像将设置为具有ID显示图像的DIV的背景。

css:

#display-image{
  width: 400px;
  height: 225px;
  border: 1px solid black;
  background-position: center;
  background-size: cover;
}

html:

<input type="file" id="image-input" accept="image/jpeg, image/png, image/jpg">

<div id="display-image"></div>

js:

const image_input = document.querySelector("#image-input");
image_input.addEventListener("change", function() {
  const reader = new FileReader();
  reader.addEventListener("load", () => {
    const uploaded_image = reader.result;
    document.querySelector("#display-image").style.backgroundImage = `url(${uploaded_image})`;
  });
  reader.readAsDataURL(this.files[0]);
});

When the input image field changes, the load event get fired. the selected image will be set as the background of the div with id display-image..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文