Use Ajax to submit a form with file upload and hide iframe applications

Source: Internet
Author: User

Generally, forms are submitted in ajax mode, so it is troublesome to upload a form with a file. The basic principle is to add a hidden iframe on the page, and then submit the form data except the file through ajax. In the callback function after the form data is submitted successfully, submit the file separately through form, the target of the form that submits the file points to the aforementioned hidden iframe.

Html code

Copy codeThe Code is as follows:
<Html>
<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"> supports uploading JPG, JPEG, GIF, BMP, SWF, RMVB, RM, and AVI files. </font>
<Iframe name = 'den den _ framework' id = "hidden_frame" style = 'display: none'> </iframe>
</Form>
</Body>
</Html>
<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>

In index.html, the main task is to write a form and iframe, and set the target of the form to the name of iframe. Be sure to set the iframe to invisible, the others are normal file upload methods. In this way, the refreshed page is the hidden Iframe, and no page is refreshed in index.html. The callback method of js is the callback method. Used to clear the File Upload box and display background information,Note that the method for clearing the File Upload box is a little different from the normal method..

Upload. jsp9Dhjsp code
Copy codeThe Code is 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 restrictions
// 1. Limit the maximum length of each uploaded file.
Su. setMaxFileSize (10000000 );
// 2. Restrict the total length of uploaded data.
Su. setTotalMaxFileSize (20000000 );
// 3. Set the files that can be uploaded (with the extension limit). Only the doc and txt files are allowed.
Su. setAllowedFilesList ("doc, txt, jpg, rar, mid, waw, mp3, gif ");
Boolean sign = true;
// 4. Set files that are not allowed to be uploaded (restricted by the extension). Do not upload files with the exe, bat, jsp, htm, and html extensions or files without extension extensions.
Try {
Su. setDeniedFilesList ("exe, bat, jsp, htm, html ");
// Upload a file
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, you only need to pay attention to the final output format. In fact, the principle is to output a piece of js Code to iframe, and then control its parent page in iframe.

OK, now a refreshing page Upload Component is ready, do not forget to add the necessary jspSmartUpload. jar package under the WEB-INF/lib.

It should be noted that Iframe is used for upload, And the status bar will be refreshed, because the page in iframe is refreshed, but the external page is that the page you see is not refreshed, so it can be said that it is 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.