Ajax iframe jsp refreshing File Upload

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
  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>

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
<! -- Page Language = "Java" contenttype = "text/html; charset = gb2312" -->
<! -- Page import = "com. jspsmart. Upload. smartupload -->

JSP code
  1. <% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %>
  2. <% @ Page import = "com. jspsmart. Upload. smartupload" %>
  3. <%
  4. // Create a new smartupload object
  5. Smartupload su = new smartupload ();
  6. // Upload Initialization
  7. Su. initialize (pagecontext );
  8. // Set upload restrictions
  9. // 1. Limit the maximum length of each uploaded file.
  10. Su. setmaxfilesize (10000000 );
  11. // 2. Restrict the total length of uploaded data.
  12. Su. settotalmaxfilesize (20000000 );
  13. // 3. Set the files that can be uploaded (with the extension limit). Only the doc and TXT files are allowed.
  14. Su. setallowedfileslist ("Doc, txt, JPG, rar, mid, Waw, MP3, GIF ");
  15. Boolean Sign = true;
  16. // 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.
  17. Try {
  18. Su. setdeniedfileslist ("EXE, bat, JSP, htm, HTML ");
  19. // Upload a file
  20. Su. Upload ();
  21. // Save the uploaded file to the specified directory
  22. Su. Save ("C ://");
  23. } Catch (exception e ){
  24. E. printstacktrace ();
  25. Sign = false;
  26. }
  27. If (Sign = true)
  28. {
  29. Out. println ("<SCRIPT> parent. Callback ('upload file SUCCESS ') </SCRIPT> ");
  30. } Else
  31. {
  32. Out. println ("<SCRIPT> parent. Callback ('upload file error') </SCRIPT> ");
  33. }
  34. %>

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.