donglang2010 2018-12-02 03:41
浏览 70
已采纳

在文件浏览器中单击“打开”时如何提交照片上传

Right now I have it as when a user clicks on the image the file browser will open up. But what I want is for after the file browser has opened and a file is chosen, once you click "open" the form will submit and the image will upload.

This is the closest I have gotten using the answer chosen here: Open File Browser on Link Click

code:

<div class="col-sm-6">
    <img height="120" width="140" id="profileImage" alt="profile-image" class="userimg" style="margin-bottom: 1rem;" onclick="document.getElementById('imageFile').click();" src="<?php echo $image_src; ?>" />
</div>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?id=<?php echo $childId; ?>" method="post" enctype="multipart/form-data" id="imageForm" name="imageForm" class="text-center">
  <!--  <input type="submit" id="btn-imageUpload" value="Submit" name="submit" /> -->
    <input type="file"  style="display:none;" id="imageFile" name="profile-photo" onchange="this.form.submit() enctype=”multipart/form-data” capture/>
</form>  

It almost worked.. When I click "open" in the file browser the page submits and refreshes, but the photo was not uploaded at all.

But it works fine if I submit using the "submit" button on the page instead of the "open" button in the file browser.

  • 写回答

1条回答 默认 最新

  • douzhenao6515 2018-12-02 03:52
    关注

    Add an event listener to your file input. Once it recognizes a change you can send them to where they need to go. Or make the request via AJAX.

    var form = document.querySelector('#profile_image'),
          input = document.querySelector('input[name="picture"]');
    
    input.addEventListener('change', e => {
      e.preventDefault();
      // form.submit(); // Uncomment this line or change it to your preferred AJAX mechanism
      document.querySelector('body').innerHTML += 'Event Listener worked!';
    });
      
    <form action="/upload/profile/<?=$childId?>" method="POST" id="profile_image">
    
      <input type="file" name="picture" />
    
    </form>

    This should easily submit your form.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键