简体   繁体   English

通过表格上传文件后如何清除输入字段

[英]How to clear an input field after uploading a file via a form

I am uploading a file using forms and onSubmit method however after submitting and clearing the input label the file is saved in some blank format and its 0KB. 我正在使用表单和onSubmit方法上传文件,但是在提交并清除输入标签后,文件以某种空白格式及其0KB保存。 When I comment out the line to clear the input label the excel file is loaded correctly. 当我注释掉该行以清除输入标签时,excel文件已正确加载。 So the main issue here is the way I am clearing that input label is wrong or I am clearing it in the wrong place. 因此,这里的主要问题是我清除输入标签错误或在错误的位置清除它的方式。

After some research I came across two errors that I was making. 经过一番研究,我遇到了两个错误。 Firstly the file was saving in a different format with 0kb size because I was clearing the input field before the actual upload happened so the program did not know the file nameor file type. 首先,文件以0kb的大小保存为另一种格式,因为我在实际上载之前就清除了输入字段,因此程序不知道文件名或文件类型。 I changed that and cleared the input field in the onSubmit function 我进行了更改,并清除了onSubmit函数中的输入字段

  <iframe width="0" height="0" border="0" name="dummyframe" 
            style="display: none;" id="dummyframe"></iframe>
  @*<form id="uploadForm" name="form1" method="post" 
            enctype="multipart/form-data" action="/api/BulkUpload" 
            target="dummyframe" onsubmit="submitFunction()">*@
  <form id="uploadForm" name="form1" method="post" 
        enctype="multipart/form-data" action="/api/BulkUpload" 
        target="dummyframe" onsubmit="return Validate(this);">
     <div>

     </div>
     <div id="inputLabel">

        <input id="fileinput" name="image1" type="file" />
     </div>
     <div>
        <span class="btn btn-success fileinput-button">
           <i class="icon-plus icon-white"></i>
           <span>upload file</span>
           <input id="submitButton" class="submit" type="submit" 
                value="ok" @*onclick="setTimeout(clearLabel,3000)"*@ />
        </span>
     </div>
  </form>





var _validFileExtensions = [".xls", ".xlsx"];
function Validate(oForm) {

    //var file = input.file[0];
    var arrInputs = oForm.getElementsByTagName("input");
    for (var i = 0; i < arrInputs.length; i++) {
       var oInput = arrInputs[i];
       if (oInput.type == "file") {

          var sFileName = oInput.value;
          if (sFileName.length > 0) {
             var blnValid = false;
             for (var j = 0; j < _validFileExtensions.length; j++) {
                var sCurExtension = _validFileExtensions[j];
                if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length)
                    .toLowerCase() == sCurExtension.toLowerCase()) {
                   blnValid = true;
                   $('#fileinput').val('');
                   break;
                }
             }

             if (!blnValid) {
                $.msgbox("Sorry, " + sFileName + " is invalid, allowed extensions are: " 
                        + _validFileExtensions.join(", "));
                return false;
             }
             //if (file.size >= 5000000)
             //if (oInput.size >= 5242880)
             if (oInput.size >= 50000)

             {

                $.msgbox("Sorry, " + sFileName + " maximum file size is 5MB ");
                return false;
             }
          }
       }
    }

    return true;

}

The way the upload could work is 上传的工作方式是

  1. User fills in form. 用户填写表格。
  2. User hits submit. 用户点击提交。
  3. App validates form data. 应用验证表格数据。 If not valid, show advice and go on with step 1. 如果无效,请显示建议并继续执行步骤1。
  4. If form valid, send data to server. 如果表格有效,则将数据发送到服务器。
  5. After success show user feedback and clear the form. 成功后,请显示用户反馈并清除表格。 (+Error handling) (+错误处理)

More of your code would be helpful, but it seems you are deleting the value of the input (step 3) before transmission (step 4). 您的更多代码会有所帮助,但是似乎您正在传输(步骤4)之前删除输入的值(步骤3)。 Better do it after a processable response (or timeout) of your request (step 5). 对请求进行可处理的响应(或超时)后,最好执行此操作(步骤5)。

I eneded up using javascript to clear the contents of the textbox after the submit has happened. 提交后,我使用javascript清除了文本框的内容。 document.getElementById("sheetName").value = "";

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

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