[英]Uploading file in a form without page refresh
I have this bit of code: 我有这段代码:
<form name="myUploadForm" method="post" action="/scripts/upload.do" enctype="multipart/form-data" id="fileUpload">
<table width="100%" border="0">
<tr>
<td>
<input type="file" name="xlsFile" size="60" value="test.xls">
<input type="button" value="Upload File" name="upload_xls">
</td>
</tr>
</table>
</form>
Right now I can upload the file with Struts but it refreshes the page. 现在我可以使用Struts上传文件,但它会刷新页面。 How do I do this without the page refreshing?
如果没有页面刷新,我该怎么做?
What worked for me: 什么对我有用:
On the form tag, I have target="hidden-iframe" 在表单标记上,我有target =“hidden-iframe”
the hidden i-frame on the page looks like this: 页面上隐藏的i-frame看起来像这样:
<iframe name="hidden-iframe" style="display: none;"></iframe>
The important thing to underline here is that the form is referencing the name attribute of the frame and not the id . 这里要强调的重要一点是表单引用了框架的name属性而不是id 。
You can post form with jQuery and get the result back. 您可以使用jQuery发布表单并返回结果。
$('#formId' ).submit( function( e ) { $.ajax( { url: '/upload', type: 'POST', data: new FormData( this ), processData: false, contentType: false, success: function(result){ console.log(result); //$("#div1").html(str); } } ); e.preventDefault(); } );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="formId" action="/upload" enctype="multipart/form-data" method="post"> <input type="text" name="title"><br> <input type="file" name="upload" multiple="multiple"><br> <input type="submit" value="Upload"> </form> <div id="div1"> </div>
如果你可以使用jQuery,你可以使用jQuery File Upload之类的东西。
There are two methods: 有两种方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.