我正在try 创建一个表单,您可以在其中使用两个file输入上传多个图像,以Base64格式查看图像,然后使用jQuery的POST提交它们.问题是,当我 Select 3个不同的图像时,我只看到最后一个图像3次.我猜问题出在addEventListener号公路中的for号公路上--也许它不在正确的位置?

<div class="form-group">
    <label for="product_photo_1">Photos 1</label>
    <div class="custom-file">
        <input type="file" class="custom-file-input" id="product_photo_1" multiple />
        <label class="custom-file-label" for="product_photo_1" data-browse="Select">Please, select a file...</label>
    </div>
    <div id="product_photo_1_result"></div>
</div>
<div class="form-group">
    <label for="product_photo_2">Photos 2</label>
    <div class="custom-file">
        <input type="file" class="custom-file-input" id="product_photo_2" multiple />
        <label class="custom-file-label" for="product_photo_2" data-browse="Select">Please, select a file...</label>
    </div>
    <div id="product_photo_2_result"></div>
</div>
<script>
    const convertBase64 = (file) => {
        return new Promise((resolve, reject) => {
            const fileReader = new FileReader();

            fileReader.readAsDataURL(file);

            fileReader.onload = () => {
                resolve(fileReader.result);
            };

            fileReader.onerror = (error) => {
                reject(error);
            };
        });
    };

    const uploadImage = async (event, id) => {
        const file = event.target.files[0];

        const base64 = await convertBase64(file);

        document.getElementById("product_photo_" + id + "_result").innerHTML += '<img src="' + base64 + '" class="img-thumbnail m-2" id="product_photo_' + id + '[]" style="height: 50px;" />';
    };

    document.getElementById("product_photo_1").addEventListener("change", (event) => {
        for(var index = 1; index <= document.getElementById("product_photo_1").files.length; index++) {
            uploadImage(event, 1);
        }
    });

    document.getElementById("product_photo_2").addEventListener("change", (event) => {
        for(var index = 1; index <= document.getElementById("product_photo_2").files.length; index++) {
            uploadImage(event, 2);
        }
    });
</script>

推荐答案

uploadImage函数中,您正在传递event个参数,并每次 Select 第一个文件

const file = event.target.files[0];

相反,您可以将文件逐个传递给uploadImage.

下面是一个例子:

<div class="form-group">
  <label for="product_photo_1">Photos 1</label>
  <div class="custom-file">
    <input type="file" class="custom-file-input" id="product_photo_1" multiple />
    <label class="custom-file-label" for="product_photo_1" data-browse="Select">Please, select a file...</label>
  </div>
  <div id="product_photo_1_result"></div>
</div>
<div class="form-group">
  <label for="product_photo_2">Photos 2</label>
  <div class="custom-file">
    <input type="file" class="custom-file-input" id="product_photo_2" multiple />
    <label class="custom-file-label" for="product_photo_2" data-browse="Select">Please, select a file...</label>
  </div>
  <div id="product_photo_2_result"></div>
</div>
<script>
  const convertBase64 = (file) => {
    return new Promise((resolve, reject) => {
      const fileReader = new FileReader();

      fileReader.readAsDataURL(file);

      fileReader.onload = () => {
        resolve(fileReader.result);
      };

      fileReader.onerror = (error) => {
        reject(error);
      };
    });
  };

  const uploadImage = async (file, id) => {
  
    const base64 = await convertBase64(file);

    document.getElementById("product_photo_" + id + "_result").innerHTML += '<img src="' + base64 + '" class="img-thumbnail m-2" id="product_photo_' + id + '[]" style="height: 50px;" />';
  };

  document.getElementById("product_photo_1").addEventListener("change", (event) => {
   let files = document.getElementById("product_photo_1").files;
    for (let index = 0; index <= files.length; index++) {
      uploadImage(files[index], 1);
    }
  });

  document.getElementById("product_photo_2").addEventListener("change", (event) => {
    let files = document.getElementById("product_photo_2").files;
    for (let index = 0; index <= document.getElementById("product_photo_2").files.length; index++) {
      uploadImage(files[index], 2);
    }
  });

</script>

Javascript相关问答推荐

可以将SuperTest导入为ES 6模块吗?

获取POS餐厅会话用户/收银员

Flutter:显示PDF和视频,但阻止下载

自定义帖子类型帖子未显示在网站上

我的glb文件没有加载到我的three.js文件中

如何判断属于多个元素的属性是否具有多个值之一

单击更新页面的按钮后,页面刷新;测试/断言超时,有两个标题,但没有一个标题

Bootstrap动态选项卡在切换选项卡后保持活动状态,导致元素堆叠

仅针对RTK查询中的未经授权的错误取消maxRetries

手机上的渲染错误文本必须在文本组件中渲染,但在浏览器上没有问题<><>

v—自动完成不显示 Select 列表中的所有项目

从包含数百行的表中获取更改后的值(以表单形式发送到后端)的正确方法是什么?

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

更新Redux存储中的对象数组

ngOnChanges仅在第二次调用时才触发

MongoDB中的嵌套搜索

使用CEPRESS截取时,cy.Wait()在等待5000ms的第一个路由请求时超时

使用API调用的VUE 3键盘输入同步问题

与在编剧中具有动态价值的定位器交互

如何在每隔2分钟刷新OKTA令牌后停止页面刷新