允许用户在没有提交按钮的情况下上传x文件夹chrome扩展上的图像

问题描述 投票:5回答:1

我想允许用户在chrome扩展示例文件夹名称(上传)上传图像而不使用提交按钮

<form action="/upload">
  <input type="file" name="myimages" accept="image/*">
</form>

显示图像

<span class="AvGbtn" id="AvBgIds" style="background-image: url(Here i want to show upload images url
); background-size: 100% 100%;" oncontextmenu="return false">  
</span>
javascript jquery html google-chrome-extension firefox-addon
1个回答
4
投票

$( document ).ready(function() {
    $( '#myimages' ).change(function() {
      var filename = $('input[type=file]').val().replace(/C:\\fakepath\\/i, '');
      $( '#myform' ).submit();
      $( '#uploaded').append('<img src="/upload/' + filename + '"/>');
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myform" action="/upload">
  <input type="file" name="myimages" id="myimages" accept="image/*">
</form>
<div id="uploaded">
</div>

使用文件上载字段的onchange事件提交表单。

编辑:

添加了在表单下方的div中显示上传图像的功能。

致谢:Use jQuery to get the file input's selected filename without the path

© www.soinside.com 2019 - 2024. All rights reserved.