我想清除我的表单中的文件输入。

我知道如何用同样的方法设置资源……但该方法不会擦除所选文件路径。

注意:我希望避免重新加载页面、重置表单或执行AJAX调用。

这可能吗?


当前回答

tl;dr:对于现代浏览器,使用即可

输入。Value = "或输入。Value = null;


旧的回答:

如何:

input.type = "text";
input.type = "file";

我仍然要理解为什么这不能与webkit工作。

无论如何,这适用于IE9>, Firefox和Opera。 webkit的情况是,我似乎无法将其更改回文件。 在IE8中,情况是它抛出一个安全异常。

编辑: 对于webkit, Opera和firefox,这是有效的:

input.value = '';

(用这个建议核对上面的答案)

我将看看是否能找到一种更好、更干净的方法来跨浏览器执行此操作,而不需要GC。

Edit2:

try{
    inputs[i].value = '';
    if(inputs[i].value){
        inputs[i].type = "text";
        inputs[i].type = "file";
    }
}catch(e){}

适用于大多数浏览器。不工作与IE < 9,这是全部。 在firefox 20, chrome 24, opera 12, IE7, IE8, IE9和IE10上测试。

其他回答

你需要用新的文件输入替换它。 下面是如何用jQuery完成的:

var inputFile = $('input[type=field]');
inputFile.wrap('<div />');

当你需要清除输入字段时使用这一行(例如在某些事件上):

inputFile.parent().html( inputFile.parent().html() );

上述答案提供了一些笨拙的解决方案,原因如下:

我不喜欢必须先包装输入,然后得到html,这是非常复杂和肮脏的。 跨浏览器JS很方便,似乎在这种情况下,有太多的未知可靠地使用类型切换(这,再次,有点脏)和设置值为“

因此,我为您提供了基于jQuery的解决方案:

$('#myinput').replaceWith($('#myinput').clone())

它说什么就做什么,它用自己的克隆来替换输入。克隆文件不会被选中。

优点:

简单易懂的代码 没有笨拙的包装或类型切换 跨浏览器兼容性(如果我说错了请指正)

结果: 快乐的程序员

帮助我的是,我尝试使用循环获取和上传最后选择的文件,而不是清除队列,它工作。这是代码。

for (int i = 0; i <= Request.Files.Count-1; i++)
{
 HttpPostedFileBase uploadfile = files[i];
 Stream fs = uploadfile.InputStream;
 BinaryReader br = new BinaryReader(fs);
 Byte[] imageBytes = br.ReadBytes((Int32)fs.Length);
}

希望这能对你有所帮助。

将值设为null或像这样的空字符串' ',以清除单个输入元素的值 您可以使用HTML按钮使用<input type= " reset " >属性轻松重置所有表单值。 HTMLFormElement.reset()方法可以恢复表单元素的默认值。此方法的作用与单击表单控件相同。

清除整个输入字段,而不必手动删除整个东西,或者如果在输入字段中已经有一个用户不想要的预先建议的输入。可能有很多情况。

您可以将此作为一个实际示例来尝试

const fileInputElement = document.getElementById('file-input'); const formElement = document.getElementById('input-form'); // Method-1: Clear files // fileInputElement.value = ''; // Method-2: Clear files // fileInputElement.value = null; // Method-3: Clear files - on event listener fileInputElement.addEventListener('change', (event)=>{ // If the file did not meet certain condition event.target.value = ''; }); // Method-4: Clear or reset whole form formElement.addEventListener('submit', (event)=>{ // If ajax call is failed to make request event.target.reset(); // or // formElement.reset() }); .form{ display: flex; flex-direction: column; width: 80%; margin-left: 10%; } #file-input, .form-control{ margin-top: 1rem; } .form-control{ padding: 1em 0; } #file-input{ background: #ccccff; } #file-input::-webkit-file-upload-button { background: #000033; color: white; padding: 1em; } .button-group{ display: flex; justify-content: start; } .btn{ background: #004d4d; color: white; width: 4rem !important; margin-right: 10px; } <form id='input-form' class='form' > <input type='file' id='file-input' /> <input type='text' class='form-control' id='text-input' placeholder='your name' /> <div class='button-group'> <button type='reset' class='form-control btn' id='reset-form'>Reset</button> <button type='submit' class='form-control btn' id='submit-form'>Submit</button> </div> </form>

不幸的是,上面的答案似乎没有一个涵盖所有的基础——至少在我用普通javascript进行的测试中没有。

.value = null似乎在FireFox, Chome, Opera和IE11(但不是IE8/9/10)上工作 FireFox上的. clonenode(和jQuery中的.clone())似乎复制了.value,因此使克隆毫无意义。

所以这里是我写的香草javascript函数,它可以在FireFox(27和28),Chrome (33), IE (8, 9, 10, 11), Opera(17)…这些是目前可供我测试的唯一浏览器。

function clearFileInput(ctrl) {
  try {
    ctrl.value = null;
  } catch(ex) { }
  if (ctrl.value) {
    ctrl.parentNode.replaceChild(ctrl.cloneNode(true), ctrl);
  }
}

ctrl参数是文件输入本身,因此该函数将被调用为…

clearFileInput(document.getElementById("myFileInput"));