Uses a hidden IFRAME to hide form submission. It is similar to Ajax and does not need to be submitted. This allows you to upload files without any need.

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

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 =" # ">
  1. <HTML>
  2. <Body>
  3. <Form action = "Upload. jsp" id = "form1" name = "form1" enctype = "multipart/form-Data" method = "Post" target = "hidden_frame">
  4. <Input type = "file" id = "file" name = "file" style = "width: 450">
  5. <Input type = "Submit" value = "Upload File"> <span id = "MSG"> </span>
  6. <Br>
  7. <Font color = "red"> supports uploading JPG, JPEG, GIF, BMP, SwF, rmvb, RM, and AVI files. </font>
  8. <IFRAME name = 'den den _ framework' id = "hidden_frame"> </iframe>
  9. </Form>
  10. </Body>
  11. </Html>
  12. <SCRIPT type = "text/JavaScript">
  13. Function callback (MSG)
  14. {
  15. Document. getelementbyid ("file"). outerhtml = Document. getelementbyid ("file"). outerhtml;
  16. Document. getelementbyid ("MSG"). innerhtml = "<font color = Red>" + MSG + "</font> ";
  17. }
  18. </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 =" # ">
  1. <! -- Page Language = "Java" contenttype = "text/html; charset = gb2312" -->
  2. <! -- PageImport= "Com. jspsmart. Upload. smartupload -->
  3. <% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %>
  4. <% @ PageImport= "Com. jspsmart. Upload. smartupload" %>
  5. <%
  6. // Create a new smartupload object
  7. Smartupload su =NewSmartupload ();
  8. // Upload Initialization
  9. Su. initialize (pagecontext );
  10. // Set upload restrictions
  11. // 1. Limit the maximum length of each uploaded file.
  12. Su. setmaxfilesize (10000000 );
  13. // 2. Restrict the total length of uploaded data.
  14. Su. settotalmaxfilesize (20000000 );
  15. // 3. Set the files that can be uploaded (with the extension limit). Only the doc and TXT files are allowed.
  16. Su. setallowedfileslist ("Doc, txt, JPG, rar, mid, Waw, MP3, GIF ");
  17. BooleanSign =True;
  18. // 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.
  19. Try{
  20. Su. setdeniedfileslist ("EXE, bat, JSP, htm, HTML ");
  21. // Upload a file
  22. Su. Upload ();
  23. // Save the uploaded file to the specified directory
  24. Su. Save ("C ://");
  25. }Catch(Exception e ){
  26. E. printstacktrace ();
  27. Sign =False;
  28. }
  29. If(Sign =True)
  30. {
  31. Out. println ("<SCRIPT> parent. Callback ('upload file SUCCESS ') </SCRIPT> ");
  32. }Else
  33. {
  34. Out. println ("<SCRIPT> parent. Callback ('upload file error') </SCRIPT> ");
  35. }
  36. %>
<! -- 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.

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.