js clears the value of input[type=file]

js cannot operate input[type=file]
but you can delete the dom element of this input, add another one, or replace it

$("#UploadFile").replaceWith('<input id="UploadFile" type="file"/>');

 

 

如果想在选同一文件上传时也会触发change事件

Need to be executed in the input's change event callback

$("#UploadFile").replaceWith('<input id="UploadFile" type="file"/>');

just replace

But you need to bind a change() event once;

 

 

ChangeBind: function () {

$("#UploadFile").change(function () {
var filePath = $(this).val();
if (filePath.indexOf("docx") != -1 || filePath.indexOf("xlsx") != -1 || filePath.indexOf("pptx") != -1) {
$(".showFileName").val(filePath);
UploadFile();
} else {
$(".showFileName").val( "");
alert("You did not upload the file, or you uploaded the wrong file type!");
return false
}
});
},

 

 


UploadFile: function (file) {
var filePath = $("#UploadFile").val();
//The regular expression gets the file name without the suffix.
var name = filePath.replace(/^.+?\\( [^\\]+?)(\.[^\.\\]*?)?$/gi, "$1");

//Regular expression to get suffix
var suffix = filePath.replace(/.+\./, "");

var fileName = name + "." + suffix;
if ($.trim(filePath) == "") {
alert("请选择需要上传的文件!");
return;
}
Common.Ajax({ Default: "FileUpload", FilePath: filePath, FileName: fileName }, function () {

}, function (data) {

}, function () {
$("#UploadFile").replaceWith('<input id="UploadFile" type="file"/>');    //Dom元素替换
ChangeBind();    //change事件绑定
});
},

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324980809&siteId=291194637