JS---of web Development using IFRAME to implement JSP without flush upload file

Source: Internet
Author: User

The first thing to say is that Ajax is unable to implement the upload file, you can think of Ajax and background communication is passed through the string, how can pass the file? In fact, for security reasons JS is unable to manipulate the file, so do not say with Ajax to implement the file upload, it is impossible.
In this paper, the implementation of the file upload is no page refresh, can be said to be an "Ajax-like" approach.
Before starting to say a few words, in fact, before the advent of Ajax, Web applications can also be non-refreshing, then mostly through the IFRAME to do this. Of course, after the advent of Ajax, people swarm to the Ajax camp, the IFrame is no more. But it's really a good choice to use an IFrame to upload files without a flush. Ps:ajax technology can basically be said to be brought up by Google, but less gmail in the uploading of files or IFRAME, so that using an IFrame to upload files is the best choice.
I am here to use the technology is JSP, in fact, asp,php, and so on is the same can be achieved
A total of two files can be achieved: index.html and upload.jsp, here to explain, the text will be attached to the source code

--index.html

HTML code
  1. "upload.jsp" id= "Form1" Name="Form1" enctype="Multipart/form-data" method= "post" target="Hidden_frame" >
  2. "file" id="file" name="file" style="width:450" >

  3. function Callback (MSG)
  4. {
  5. document.getElementById ("file"). outerHTML = document.getElementById ("file"). outerHTML;
  6. document.getElementById ("msg"). InnerHTML = "" +msg+ "";
  7. }



The main thing to do in index.html is to write a form and an IFRAME, and set the target of the form as the name of the IFRAME, note that the IFRAME is not visible, the other is the normal file upload, so the refreshed page is this hidden Ifra Me, and in index.html there is no page refresh, JS callback method is the callback method. Used to empty the File upload box and display background information, note the method of emptying the File upload box, and the normal method is a little different.

--upload.jsp

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 limit
  9. //1. Limit the maximum length of each uploaded file.
  10. Su.setmaxfilesize (10000000);
  11. //2. Limit the length of the total upload data.
  12. Su.settotalmaxfilesize (20000000);
  13. //3. Set the allowed files to be uploaded (via extension restrictions), allowing only doc,txt files.
  14. Su.setallowedfileslist ("doc,txt,jpg,rar,mid,waw,mp3,gif");
  15. boolean sign = true;
  16. //4. Set files that are blocked from uploading (via extension restrictions), prohibit uploading files with exe,bat,jsp,htm,html extension and files without extensions.
  17. try {
  18. Su.setdeniedfileslist ("exe,bat,jsp,htm,html");
  19. //Upload files
  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 ("");
  30. }else
  31. {
  32. Out.println ("");
  33. }
  34. %>





In the upload.jsp, just pay attention to the final output format. In fact, the principle is to output a section of JS code into the IFRAME, and then in the IFrame to control its parent page.

OK, this is a no-refresh page upload component is done, do not forget to add the necessary Jspsmartupload.jar package under the Web-inf/lib.
Need to explain is to use the IFRAME to upload, the status bar will still have a refresh, because the page in the IFRAME refresh, but the external page, is that you see the page is not refreshed, so it can be said to be similar to Ajax upload.

http://blog.csdn.net/foart/article/details/6622278

JS---of web Development using IFRAME to implement JSP without flush upload file

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.