After googled many methods, there are several methods on the Internet:
1. Replace the original input box
2. Remove the original input box, and then add a new same input box
But don't know why very unfortunately, I can't get it to work. .
Later, an inspiration suddenly flashed in my head. After I triggered an onchange event, he did not respond. Is the onchange event not bound? Later, I found that my speculation was correct, and then I bound an onchange to him again. Event, test passed! ! !
problem solved! !
- $("#targetFile").change(function(){
- var filename = $(this).val();
- $("#originalTargetFileName").val(filename);
- });
- $("#targetUpload").submit(function(){
- $.ajaxFileUpload({
- type: "post",
- url: "${pageContext.request.contextPath}/upload.do",
- secureuri:false,
- fileElementId:"targetFile",
- dataType: "json",
- success: function(result,status) {
- if (result.success == "1") {
- alert( "Upload file successfully!" );
- var filename=getFileNameFromFilePath(result.fileRelativePath);
- $("#target_upload_info").html("<div>"+"文件:"+filename+" <a href='javascript:void(0)' onclick='deletefile("+"\""+result.fileRelativePath+"\",\"target\")'>删除</a>"+"<br/></div>");
- $("#target_upload_info").css("visibility", "visible");
- $("#targetFileRelativePath").val(result.fileRelativePath);
- } else {
- $( "#target_upload_info" ).html( "File upload failed: " + result.msg);
- $("#target_upload_info").css({"visibility":"visible", "color":"red"});
- }
- },
- complete: function(xmlHttpRequest) {
- $("#targetFile").replaceWith('<input type="file" id="targetFile" name="upFile" style="display:none;"/>');
- $("#targetFile").on("change", function(){
- var filename = $(this).val();
- $("#originalTargetFileName").val(filename);
- });
- },
- error: function(data, status, e) {
- alert( "File upload failed!" );
- }
- });
- returnfalse;
- });