Upload without refreshing files
Ajax submit a form that contains files
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>
- <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. getelementbyid ("file"). outerhtml = Document. getelementbyid ("file"). outerhtml;
- Document. getelementbyid ("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. jsp
JSP code
- <% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %>
- <% @ PageImport= "Com. jspsmart. Upload. smartupload" %>
- <%
- // Create a new smartupload object
- Smartupload su =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 ");
- BooleanSign =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.