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
I took a detour while writing IFRAME. I didn't set the IFRAME name. The hateful vs2008 prompts that the HTML tag <IFRAME> has no name attribute !!!
The target jump Method of form calls name.5555555555555555555.
When uploading files, you must note that the HTML file tag should also assign a value to the name. Otherwise, after the post is uploaded, the server cannot find the file object.
HTML code {
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
DP. Sh. toolbar. copytoclipboard (this); Return false;
}
}
}
} "Href =" # ">
- <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"> </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>
<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 "> JPG, JPEG, GIF, BMP, SwF, rmvb, RM, AVI File Upload </font> <IFRAME name = 'den den _ framework' id = "hidden_frame"> </iframe> </form> </body>
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
Java code {
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
DP. Sh. toolbar. copytoclipboard (this); Return false;
}
}
}
} "Href =" # ">
- <! -- Page Language = "Java" contenttype = "text/html; charset = gb2312" -->
- <! -- PageImport= "Com. jspsmart. Upload. smartupload -->
- <% @ 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> ");
- }
- %>
<! -- Page Language = "Java" contenttype = "text/html; charset = gb2312" --> <! -- Page import = "com. jspsmart. upload. smartupload --> <% @ page Language = "Java" contenttype = "text/html; charset = gb2312" %> <% @ page import = "com. jspsmart. upload. smartupload "%> <% // create a new smartupload object smartupload su = new smartupload (); // upload and initialize Su. initialize (pagecontext); // sets the upload limit. // 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 (restricted by the extension). Only the doc and TXT files are allowed. Su. setallowedfileslist ("Doc, txt, JPG, rar, mid, Waw, MP3, GIF"); Boolean Sign = true; // 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. Try {Su. setdeniedfileslist ("EXE, bat, JSP, htm, HTML"); // upload the 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.