<script src= "/scripts/ajaxfileupload.js" ></script>
<script src= "/scripts/jquery2.1.1.min.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ (": Button"). Click (function () {
if ($ ("#file1"). Val (). length > 0) {
Ajaxfileupload ();
}
else {
Alert ("Please select Picture");
}
})
})
function Ajaxfileupload () {
$.ajaxfileupload
(
{
URL: '/api/poi/picture-upload ',//server-side request address for file upload
Type: ' Post ',
Secureuri:false,//general set to False
Fileelementid: ' file1 ',//File upload Space id attribute <input type= "file" id= "file" name= "file"/>
DataType: ' json ',//return value type is generally set to JSON
Success:function (data, status)//Server Success Response handler function
{
Console.log (data);
$ ("#img1"). attr ("src", data.imgpath1);
if (typeof (Data.error)! = ' undefined ') {
if (data.error! = ") {
alert (data.error);
} else {
alert (data.msg);
}
}
},
Error:function (data, status, E)//server response failure handler function
{
Console.log (data);
}
}
)
return false;
}
</script>
Ajaxfileupload the upload succeeds but executes error: there is more information in the returned JSON data causing
Add this piece to the Ajaxfileupload.js:
if (type = = "json")
var start = Data.indexof (' {');
var end = Data.indexof ('} ') +1;
data = data.substring (start,end);
eval ("data =" + data);
Ajaxfileupload reported this error jquery.handleerror is not a function: version issue.
Add this piece to the Ajaxfileupload.js:
Handleerror:function (S, xhr, status, E) {
If a local callback is specified, fire it
if (s.error) {
S.error.call (S.context | | s, XHR, status, E);
}
Fire the global callback
if (S.global) {
(S.context jQuery (s.context): jquery.event). Trigger ("Ajaxerror", [XHR, S, E]);
}
}
Ajax uploading files and frequently asked questions in use