Turn from: https://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html
The Ajaxfileupload of jquery plug-ins
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://files.cnblogs.com/files/kissdodog/ajaxfileupload_JS_File.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 errors
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 () {$ (": BU
Tton "). Click (function () {ajaxfileupload ();
}) function Ajaxfileupload () {$.ajaxfileupload (}) {
URL: '/upload.aspx ',//server-side request address for file uploads Secureuri:false,//Whether a security protocol is required, generally set to False Fileelementid: ' file1 ',//File upload domain ID dataType: ' JSON ',//return value type is generally set to JSON Succe Ss:function (data, status)//server succeeded in responding to the processing function {$ ("#img1"). attr ("src", Data.imgur
L);
if (typeof (Data.error)!= ' undefined ') {if (Data.error!= ') {
alert (data.error); else {alert (data.msg);
}}, Error:function (data, status, E)//server response failure Handler
{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