From: http://blog.sina.com.cn/s/blog_677b66170100lnek.html
You need to use the jquery library file and the ajaxfileupload Library File
Use instance
Http://www.phpletter.com/contents/ajaxfileupload/ajaxfileupload.js
1. Contains the file section
- <SCRIPT type = "text/JavaScript" src = "jquery. js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = "ajaxfileupload. js"> </SCRIPT>
Ii. html Section
- Loading"Src =" loading.gif "style =" display: none; ">
- <Input id ="Filetoupload"Type =" file "size =" 20 "name ="Filetoupload"Class =" input ">
- <Button class = "button" id = "buttonupload" onclick = "ReturnAjaxfileupload(); "> Upload </button>
You only need three elements, one dynamic loading small icon, one file domain, and one button.
Note: you do not need to use the ajaxfileupload plug-in to upload a file:
- <Form name = "form" Action = "" method = "Post" enctype = "multipart/form-Data">
- ...... Related HTML code ......
- </Form>
Because the ajaxfileupload plug-in automatically generates a form submission form.
For the file domain ID and name, The fileelementid parameter of the ajaxfileupload plug-in needs to obtain the file domain ID. If you want to process the file upload operation, you need to know the file domain name to obtain the information of the file to be uploaded, the relationship between the two must be clear.
Iii. Javascript
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML>
Description of main parameters:
1. the URL indicates the file path for processing the file upload operation. You can test whether the URL can be accessed directly in the browser. For example, upload. php
2. fileelementid indicates the file domain ID, as shown above: filetoupload
3. Whether secureuri enables secure submission. The default value is false.
4. datatype data. Generally, the original structure of JSON and JavaScript is selected.
5. success: the post-processing function is successfully submitted.
6. Error submission failure Handler
There are two methods above. A small dynamic loading icon prompts the loading () function and the ajaxfileupload file to upload $. ajaxfileupload () function, which corresponds to jquery. ajax () functions are similar and easy to use. Here I omit PHP to process uploaded files. Using the jquery plug-in ajaxfileupload to implement Ajax files is that simple.
At the same time, we need to know the relevant error messages
1. syntaxerror: missing; before statement Error
If this error occurs, check whether the URL path can be accessed.
2. syntaxerror: syntax error
If this error occurs, check whether the PHP file for processing the submitted operation has a syntax error.
3, syntaxerror: Invalid property ID Error
If this error occurs, check whether the property ID exists.
4, syntaxerror: missing} in XML expression Error
If this error occurs, check whether the file domain name is consistent or does not exist.
5. other custom errors
You can directly print the variable $ error to check whether the parameters are correct, which is much more convenient than the above error prompts.
Upload. php
$ Error = 'error description define ';
$ MSG = 'value to be returned ';
Echo "{";
Echo "Error: '". $ error. "', \ n ";
Echo "MSG: '". $ msg. "' \ n ";
Echo "}";
Note that you want to return the value $ MSG,$ Error value must be blank