jquery Plugin Ajaxfileupload Use instance to parse _jquery

Source: Internet
Author: User
Tags commit httpcontext

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>

Fourth Step: background page upload.aspx code:

    protected void Page_Load (object sender, EventArgs e)
    {
      httpfilecollection files = request.files;
      String msg = string. Empty;
      String error = String. Empty;
      string Imgurl;
      if (Files. Count > 0)
      {
        files[0]. SaveAs (Server.MapPath ("/") + System.IO.Path.GetFileName (files[0). FileName));
        msg = "Success!" The file size is: "+ files[0]." ContentLength;
        Imgurl = "/" + files[0]. FileName;
        string res = "{error: '" + Error + "', msg: ' + msg +" ', Imgurl: ' "+ Imgurl +" '} ";
        Response.Write (res);
        Response.End ();
      }
    


This example complete code download

An example of an MVC version:

Controller code

  public class Homecontroller:controller
  {public
    actionresult Index ()
    {return
      View ()
    }

    Public ActionResult Upload ()
    {
      httpfilecollection HFC = System.Web.HttpContext.Current.Request.Files;
      String imgpath = "";
      if (HFC. Count > 0)
      {
        Imgpath = "/testupload" + hfc[0]. FileName;
        String physicalpath = Server.MapPath (Imgpath);
        Hfc[0]. SaveAs (PhysicalPath);
      }
      Return Content (Imgpath);
    }
  

Front view, HTML and JS code, after successful upload, return the real address of the picture and bind to the src address

2016-07-28, an error in the comment: TypeError: $.ajaxfileupload is not a function we are not using the same JS, you have to go to another ajaxfileupload.

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.