Example of submitting a form with file upload using jsp ajax

Source: Internet
Author: User
Tags file upload

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

The code is as follows: Copy code

<Html>
<Body>

<Form action = "upload. jsp" id = "form1" name = "form1" encType = "multipart/form-data" method = "post" target = "hidden_frame">
<Input type = "file" id = "file" name = "file" style = "width: 450">
<INPUT type = "submit" value = "Upload file"> <span id = "msg"> </span>
<Br>
<Font color = "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>

<Script type = "text/javascript">
Function callback (msg)
{
Document. getElementByIdx_x_x ("file"). outerHTML = document. getElementByIdx_x_x ("file"). outerHTML;
Document. getElementByIdx_x_x ("msg"). innerHTML = "<font color = 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, others are normal file upload methods. The refreshed page is the hidden Iframe, but no page is refreshed in index.html. The callback method of js is the callback method. It is used to clear the file upload box and display background information. Note that the method of clearing the file upload box is a bit different from the normal method.

-- Upload. jsp9Dhjsp code

The code is as follows: Copy code

<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
<% @ Page import = "com. jspsmart. upload. SmartUpload" %>

<%
// Create a new SmartUpload object
SmartUpload su = new SmartUpload ();

// 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 file success ') </script> ");
} Else

Out. println ("<script> parent. callback ('upload file error') </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.