简体   繁体   English

是否可以使用JavaScript删除输入类型=文件选择?

[英]Is it possible to remove input type=file selections with JavaScript?

If you have an HTML form with an element like this: 如果您的HTML表单包含如下元素:

<input type="file" name="file" multiple="multiple"> 

Then normally it would allow the user to select multiple files to upload, and they all get sent to the server with the same form field name "file". 然后通常它将允许用户选择要上传的多个文件,并且它们都被发送到具有相同表单字段名称“file”的服务器。

I understand that due to security reasons, browsers won't let JavaScript just set any value for the selected files, and that's a good thing. 据我所知,由于安全原因,浏览器不会让JavaScript只为所选文件设置任何值,这是一件好事。

I just wonder though, once the user has selected the files, I would like to have multiple forms perform the uploads individually. 我只是想知道,一旦用户选择了文件,我希望有多个表单单独执行上传。 Is there a way to either: 有没有办法:

  • Clone the input item into each new form, and programatically remove file selections from each element? 将输入项克隆到每个新表单中,并以编程方式从每个元素中删除文件选择?
  • Transfer some of their selections to a separate input type=file element, or even a different form on the screen? 将他们的一些选择转移到单独的input type = file元素,甚至是屏幕上的其他表单?

If you clone an input[type=file] element, the clone's value is blank. 如果克隆input[type=file]元素,则克隆的值为空。 You also cannot copy the value from one file input to another. 您也无法将值从一个文件输入复制到另一个文件。

I've never tried to move a file input from one document to another after the file is selected. 在选择文件后,我从未尝试文件输入从一个文档移动到另一个文档。 I'd like to think the browser would clear the selection when you did that, too, but it may not. 我想,当你这样做时,浏览器会清除选择,但它可能不会。 (Update: Of three browsers tested, all of them retained the value, to my surprise. See below.) (更新:在测试的三个浏览器中,所有这些都保留了这个值,令我惊讶。见下文。)

Fundamentally, if you want to use separate forms to upload the files individually, you'll want the inputs in separate forms to begin with, before the user makes their selections. 从根本上说,如果您想使用单独的表单单独上传文件,您需要在用户进行选择之前以单独的形式开始输入。 But apparently you may be able to move them, which would let you do what you want to do. 但显然你可以移动它们,这可以让你做你想做的事。 I'd still probably start out with separate forms, to avoid having to move them. 我仍然可能从单独的表格开始,以避免不得不移动它们。


Interestingly, Chrome, Firefox, and IE10 at least seem perfectly happy to move a file element with its selection intact, even to a different document: Live Example | 有趣的是,Chrome,Firefox和IE10至少看起来非常高兴移动文件元素,其选择完好无损,即使是另一个文档: Live Example | Source 资源

HTML: HTML:

<input id="theFile" type="file">
<br>Choose a file above, then either:
<br><input type="button" id="btnClone" value="Clone">
<input type="button" id="btnMove" value="Move to iframe">
<br>
<iframe id="theFrame" style="width: 99%"></iframe>

JavaScript: JavaScript的:

(function() {
  gid("btnClone").onclick = function() {
    display("Here's the clone:");
    document.body.appendChild(gid("theFile").cloneNode(true));
  };
  gid("btnMove").onclick = function() {
    gid("theFrame").contentDocument.body.appendChild(gid("theFile"));
    display("File input moved to the iframe");
  };

  function gid(id) {
    return document.getElementById(id);
  }

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = String(msg);
    document.body.appendChild(p);
  }
})();

With the above, if I select something and click the Move to iframe button, the input is moved, apparently intact. 有了上述内容,如果我选择了某些内容并单击Move to iframe按钮,则输入将被移动,显然完好无损。 (Cloning clears the value, as expected.) (正如预期的那样,克隆会清除该值。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM