jquery plugin ajaxfileupload asynchronous upload file _jquery

Source: Internet
Author: User
Tags commit

Ajaxfileupload.js Many of the same names because making it easy.
This is what I'm using :https://github.com/carlcarl/AjaxFileUpload.
Download the address here: Http://xiazai.jb51.net/201610/yuanma/ajaxfileupload (jb51.net). rar

Ajaxfileupload.js is not a very well-known plug-ins, but others write a good release for everyone to use, the principle is to create hidden forms and IFRAME and then use JS to submit, get the return value.

I did a function of asynchronous upload, select it because it is configured in a more like jquery Ajax, I like it.

Not in the comments. That's because we're not using the same JS. I github search ajaxfileupload out a lot like JS.

Ajaxfileupload is a jquery plugin for an asynchronous upload file

Send a don't know what version of up, later do not have to look everywhere.

Syntax: $.ajaxfileupload ([options])

Options parameter Description:

1, URL upload processing program address.
2,fileelementid the ID of the file domain that needs to be uploaded, that is, <input type= "file" >.
3,secureuri whether security commits are enabled and false by default.
The type of data returned by the 4,datatype server. can be for xml,script,json,html. If you do not fill in, jquery will automatically judge.
5,success the processing function that is executed automatically after the commit is successful, and the parameter data is the server returns.
6,error the processing function that the commit failed to perform automatically.
7,data custom parameters. This thing is more useful, when the data is related to the uploaded pictures, this thing will be used.
8, type when the custom parameter is to be submitted, this parameter is set to post

Error Tip:

1, syntaxerror:missing; Before statement error
If this error occurs, you need to check that the URL path is accessible
2, syntaxerror:syntax error errors
If this error occurs, you need to check for syntax errors in the server spooler that handles the commit operation
3, Syntaxerror:invalid Property ID Error
If this error occurs, check to see if the Text field property ID exists
4, syntaxerror:missing} in XML expression error
If this error occurs, you need to check that the file name is consistent or not present
5, other custom error
You can use the variable $error Direct printing method to check whether the parameters are correct, compared to these invalid error tips or more convenient.

How to use:

First step: jquery and Ajaxfileupload plug-ins are introduced first. Note The order, this is needless to say, all plug-ins are the case.

 <script src= "Jquery-1.7.1.js" type= "Text/javascript" ></script>
 <script src= "Ajaxfileupload.js" Type= "Text/javascript" ></script>

Step Two: HTML code:

<body>
 <p><input type= "file" id= "file1" name= "file"/></p> <input
 "button" value= "Upload"/> <p></p>
</body>

Step Three: JS Code

 <script src= "Jquery-1.7.1.js" type= "Text/javascript" ></script>
 <script src= "Ajaxfileupload.js" Type= "Text/javascript" ></script>
 <script type= "Text/javascript" >
 $ (function () {
 $ (": Button "). Click (function () {
 ajaxfileupload ();
 })
 })
 function Ajaxfileupload () {
 $.ajaxfileupload
 (
 {
  URL: '/upload.aspx '),//server-side request address
  for file upload Secureuri:false,//Whether a security protocol is required, generally set to False
  Fileelementid: ' file1 ',//File upload domain ID
  dataType: ' json ',//return value type The general settings are JSON
  success:function (data, status)/Server Success Response handler function
  {
  $ ("#img1"). attr ("src", data.imgurl);
  if (typeof (Data.error)!= ' undefined ') {
  if (data.error!= ') {
  alert (data.error);
  } else {
  alert (d ata.msg);
  }
  }
  ,
  error:function (data, status, E)//server response failure processing function
  {
  alert (e);
  }
 }
 )
 return false;
 }
 </script>

A second example

Use ajaxfileupload upload files, sometimes need to submit with parameters, the Internet has a lot of data said to use data, but in fact, to use data with parameters is needed to modify, otherwise the background is not available.

Analysis Reason:

Ajaxfileupload in order to implement the asynchronous submit file without flushing, build the IFRAME and then create the form form and then submit the file to be uploaded. But the original code did not process data. So this piece of content needs to be added to ourselves.
The red section below is for modifying three places of Ajaxfileupload.js:

Note: It seems that the CSDN code in the color plus the problem of these three changes in the place I directly posted, the location of the following code,

①createuploadform:function (ID, fileelementid,data);

②if (data) {
for (var i in data) {
$ (' <input type= "hidden" name= "' + i + '" value= "' + data[i] + '"/> '). Appendto (form);
}
}

③var form = jquery.createuploadform (ID, s.fileelementid,s.data);

Ajaxfileupload in order to implement the asynchronous submit file without flushing, build the IFRAME and then create the form form and then submit the file to be uploaded. But the original code did not process data. So this piece of content needs to be added to ourselves.
The red section below is for modifying three places of Ajaxfileupload.js:
Note: It seems that the CSDN code in the color plus the problem of these three changes in the place I directly posted, the location of the following code,

Createuploadform:function (ID, fileelementid,data);

if (data) {for 
 (var i in data) { 
 $ (' <input type= "hidden" name= "' + i + '" value= "' + data[i] + '"/> '). AppE Ndto (form);
 }

var form = Jquery.createuploadform (ID, s.fileelementid,s.data);


How do you use it?

$.ajaxfileupload ({
URL: ' Http://localhost:8080/HNUST/crawler/ordinary2 ', 
type: ' Post ',
data: {
Url:url,
Keyword:keyword,
rule:rule,
data:data,
sign:sign
},
Secureuri:false,// General set to False
Fileelementid: ' file ',///upload file ID, name attribute name
dataType: ' JSON ',//return value type, generally set to JSON, Application/json This is not going to get the returned data
success:function. { 
alert (data),
error:function, Status, E) { 
alert (e);
}
}); 

Html:
<input type= "file" id= "file" name= "file" >
Java:java Background fetch parameters or normal access:

String url=request.getparameter ("url");
String keyword=request.getparameter ("keyword");

Get file and branch read (not picture):

Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request; 
Multipartfile multipartfile = multipartrequest.getfile ("file"); This file is to be consistent with Fileelementid

try {
inputstream inputstream=multipartfile.getinputstream ();
BufferedReader reader = new BufferedReader (new InputStreamReader (InputStream));
String tempstring = null;

Read one row at a time until you read null for file end while
((tempstring = Reader.readline ())!= null) {
filelist.add (tempstring); 
}
} catch (IOException E2) {
//TODO auto-generated catch block
e2.printstacktrace ();
}

There are two points to note in the use process:
Firstly, datatype must be capitalized;
Second, the value of data should be written in JSON format, otherwise the background cannot accept parameters

For more highlights, please click on the "jquery Upload operation Summary" for in-depth study and research.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.