Using IFRAME to simulate Ajax submission instance in JS

Source: Internet
Author: User
Tags file upload

HTML page code

The code is as follows Copy Code

<iframe name= "iframe" style= "Display:none" ></iframe>
<form action= "action.php" method= "post" enctype= "Multipart/form-data" target= "iframe" >
</form>

function Errors (msg)
{
Alert (msg);
}


action.php page

The code is as follows Copy Code

if ($input = = ") {
echo "<script>parent.errors (' this is a test!!! ');</script> ";
Exit
}


So that when we submit it will pop up this is a test!!! And if you want to have a nice pop-up layer effect we can put

function Errors (msg)
{
Alert (msg);
}

function into the JS pop-up layer can be.


example, using IFRAME imitate Ajax file upload

HTML page

The code is as follows Copy Code
<body>
<form action= "upload.jsp" id= "Form1" Name= "Form1" enctype=
Data "method=" POST "target=" Hidden_frame ">
<input type= "File" id= "file" name= "file" style= "width:450" >
<inputtype= "Submit" value= "Upload file" >
</form>
<iframe name= "Hidden_frame" id= "Hidden_frame" style= "Display:none" ></iframe><span id= "MSG" ></ Span>
</body>
<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>

JS Processing page

The code is as follows Copy Code

<%@ page language= "java" contenttype= "text/html; charset=gb2312 "%>
<%@ page import= "Com.jspsmart.upload.SmartUpload"%>
<%
Create a new Smartupload object
Smartupload su = new Smartupload ();
Upload initialization
Su.initialize (PageContext);
Set upload limit
1. Limit the maximum length of each uploaded file.
Su.setmaxfilesize (10000000);
2. Limit the length of the total upload data.
Su.settotalmaxfilesize (20000000);
3. Set the file allowed to upload (by extension limit), only allow doc,txt files.
Su.setallowedfileslist ("Doc,txt,jpg,rar,mid,waw,mp3,gif");
Boolean sign = true;
4. Set the file that prohibits uploading (by extension limit), prohibit uploading files with exe,bat,jsp,htm,html extensions and files with no extension.
try {
Su.setdeniedfileslist ("exe,bat,jsp,htm,html");

Uploading files
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>");
}
%>

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.