Ajax JSP does not need to upload any new files

Source: Internet
Author: User

The first thing I want to talk about is that AJAX cannot upload files. I can think about how AJAX can transfer files through passing strings to communicate with the background? In fact, JS cannot operate files for security reasons. Therefore, it is impossible to use ajax to upload files.
The file upload implemented in this article also has no page refresh, which can be said to be a "Ajax-like" method.
Before the beginning, I would like to say that two sentences are irrelevant. In fact, before the emergence of Ajax, web applications can also be refreshing. At that time, most of them were implemented through IFRAME. Of course, after the emergence of Ajax, people rushed to the Ajax camp, and IFRAME was no longer interesting. However, it is a good choice to use IFRAME to upload files without refreshing. PS: Ajax technology is basically brought up by Google, but IFRAME is not used for uploading files in Gmail. Therefore, using IFRAME to upload files is the best choice.
The technology I use here is JSP. In fact, ASP and PHP can also be implemented in this way.
A total of two files can be implemented: index.html and upload. jsp. Here, we will describe the source code.

--Index.html

HTML code

<HTML> <br/> <body> </P> <p> <form action = "upload. JSP "id =" form1 "name =" form1 "enctype =" multipart/form-Data "method =" Post "target =" hidden_frame "> <br/> <input type = "file" id = "file" name = "file" style = "width: 450 "> <br/> <input type =" Submit "value =" Upload File "> <span id =" MSG "> </span> <br/> <br> <br/> <font color = "red"> supports JPG, JPEG, GIF, BMP, SwF, rmvb, RM, AVI File Upload </font> <br/> <IFRAME name = 'den den _ framework' id = "hidden_frame" style = 'display: none '> </iframe> <br/> </form> </P> <p> </body> <br/> </ptml> </P> <p> <SCRIPT type = "text/JavaScript"> <br/> function callback (MSG) <br/>{< br/> document. getelementbyid ("file "). outerhtml = document. getelementbyid ("file "). outerhtml; <br/> document. getelementbyid ("MSG "). innerhtml = "<font color = Red>" + MSG + "</font>"; <br/>}< br/> </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. jsp

JSP code<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %> <br/> <% @ page import = "com. jspsmart. upload. smartupload "%> </P> <p> <% <br/> // create a smartupload object <br/> smartupload su = new smartupload (); </P> <p> // upload initialization <br/> Su. initialize (pagecontext); </P> <p> // sets the upload restriction. <br/> // 1. limit the maximum length of each uploaded file. <Br/> Su. setmaxfilesize (10000000); </P> <p> // 2. Restrict the total length of uploaded data. <Br/> Su. settotalmaxfilesize (20000000); </P> <p> // 3. Set files that can be uploaded (by extension restrictions). Only Doc and TXT files are allowed. <Br/> Su. setallowedfileslist ("Doc, txt, JPG, rar, mid, Waw, MP3, GIF"); </P> <p> Boolean Sign = true; </P> <p> // 4. sets files that are not allowed to be uploaded (by extension restrictions). Files with EXE, bat, JSP, htm, and HTML extensions and files without extension extensions are prohibited to be uploaded. <Br/> try {<br/> Su. setdeniedfileslist ("EXE, bat, JSP, htm, HTML"); </P> <p> // upload a file <br/> Su. upload (); <br/> // Save the uploaded file to the specified directory <br/> Su. save ("C: //"); </P> <p >}catch (exception e) {<br/> E. printstacktrace (); <br/> Sign = false; <br/>}< br/> If (Sign = true) <br/>{< br/> out. println ("<SCRIPT> parent. callback ('upload file SUCCESS ') </SCRIPT> "); <br/>}else <br/>{< br/> out. println ("<SCRIPT> parent. callback ('upload file error') </SCRIPT> "); <br/>}< br/>%> <br/>

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.