Ajax way to submit the form with file upload and hide iframe application _ajax Related

Source: Internet
Author: User

General forms are submitted through Ajax, so it's more cumbersome to run into a form with a file upload. The basic principle is to add a hidden iframe on the page, and then submit the form data in addition to the file via Ajax, and in the callback function after the form data is submitted successfully, the file is submitted separately by form, and the target of the form of the submitted file points to the hidden iframe as described above.

HTML code

Copy Code code as follows:

<body>
<form action= "upload.jsp" id= "Form1" Name= "Form1" enctype= "Multipart/form-data" method= "post" target= "Hidden_ Frame ">
<inputtype= "File" id= "file" name= "file" style= "width:450" >
<inputtype= "Submit" value= "Upload file" ><spanid= "MSG" ></span>
<br>
<fontcolor= "Red" > Support Jpg,jpeg,gif,bmp,swf,rmvb,rm,avi File Upload </font>
<iframe name= ' hidden_frame ' id= ' hidden_frame ' style= ' Display:none ' ></iframe>
</form>
</body>
<scripttype= "Text/javascript" >
Functioncallback (msg)
{
document.getelementbyidx_x_x ("file"). outerHTML =document.getelementbyidx_x_x ("file"). outerHTML;
Document.getelementbyidx_x_x ("msg"). InnerHTML = "<fontcolor=red>" +msg+ "</font>";
}
</script>

Index.html in the main to do is to write a form and IFRAME, and the form of target set as the name of the IFRAME, pay attention to the IFRAME is not visible, the other is the normal file upload, so that the refresh page is this hidden iframe , and in the index.html is not a page refresh, JS callback method is callback method. For emptying the File upload box and displaying background information, note the way to empty the file upload box is a little different from the normal method.

upload.jsp9dhjsp Code
Copy Code code as follows:

<%@ page language= "java" contenttype= "text/html; charset=gb2312 "%>
<%@ pageimport= "Com.jspsmart.upload.SmartUpload"%>
<%
Create a new Smartupload object
SMARTUPLOADSU = Newsmartupload ();
Upload initialization
Su.initialize (PageContext);
Set upload limit
1. Limit the maximum length of each uploaded file.
Su.setmaxfilesize (10000000);
2. Limit the length of the total upload data.
Su.settotalmaxfilesize (20000000);
3. Set the file allowed to upload (by extension limit), only allow doc,txt files.
Su.setallowedfileslist ("Doc,txt,jpg,rar,mid,waw,mp3,gif");
Boolean sign= true;
4. Set the file that prohibits uploading (by extension limit), prohibit uploading files with exe,bat,jsp,htm,html extensions and files with no extension.
try{
Su.setdeniedfileslist ("exe,bat,jsp,htm,html");
Uploading files
Su.upload ();
Save the uploaded file to the specified directory
Su.save ("c://");
catch (Exception e) {
E.printstacktrace ();
sign = false;
}
if (sign==true)
{
Out.println ("<script>parent.callback (' upload filesuccess ') </script>");
}else
{
Out.println ("<script>parent.callback (' Uploadfileerror ') </script>");
}
%>

In upload.jsp, just pay attention to the final output format. In fact, the principle is to output a section of JS code into the IFRAME, and then in the IFrame to control its parent page.

OK, this is not a refreshing page upload component is done, do not forget to add the necessary Jspsmartupload.jar package under the Web-inf/lib.

Need to explain the use of IFrame to upload, the status bar will still have a refresh, because the page in the IFRAME refresh, but the external page, is that you see the page is not refreshed, so it can be said to be similar to Ajax upload

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.