Method for uploading files without refreshing

Here is the method of using jquery.form.js

 

1. First, we must define a form for file upload, such as:

 

<form action="${basePath }/purchasing/supplier!uploadAttach.do" onsubmit="return check()" id="form1" encType="multipart/form-data"  method="post">
	<input type="hidden" name="id" value="${id }">
	<table height=8 width="100%"
bordercolor="#CCBE5A" cellspacing="0"
                        bordercolordark="#ffffff" cellpadding="3"
                        align="center" bgcolor="#ffffff" bordercolorlight="#7f9db9"
                        border="1">    
       <TR>
       <td BGCOLOR="#d3d8eb"><font size="2">文件</font></td>
       <td><input type="file" id="file" name="file" style="width:450"></td>
       </TR>
       <TR>
       <td BGCOLOR="#d3d8eb"><font size="2">备注</font></td>
       <td><textarea name="remark"></textarea></td>
       </TR>
     </table>
	
	
</form>

 

2. Then use javascript code:

 

$("#uploadBtn").click(function(){
		$("#form1").ajaxSubmit({
			success: function (data) {
	        	if(data.success){
	        		alert("Upload successful");
	        		if(window.opener){
	        			window.opener.location.reload();
	        		}
	        		window.close();
	        	}else{
	        		alert("Upload failed");
	        	}
	        },
	        error: function (error) { alert("Upload exception"); },
	        type: "post", /*Set the form to submit by post method*/
	        dataType: "json" /*Set the return value type to text*/
		})
	})

 

Guess you like

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