The JS code of the view page where the return value is to be the serial number datatype to capitalize
<script type= "Text/javascript" > function Ajaxfileupload () { $.ajaxfileupload ({ URL: '/fileupload /index ',//server-side request address Secureuri:false for file upload,// Whether security protocol is required, general set to False Fileelementid: ' FileName ',//file upload domain ID dataType: ' json ',//return value type is generally set to JSON success:function (data, status)// Server Success Response handler function { var datas = $.parsejson (data)//Here Be sure to serialize $ ("#img_url"). Val (Datas.img_url); }, error:function (data)// Server response failed handler { alert (data.img_url); } } ) return false;} </script>
Ajaxfileupload Code
Jquery.extend ({createuploadiframe:function (id, URI) {//create frame var Frameid = ' juploadframe ' + ID; var iframehtml = ' <iframe id= ' + Frameid + ' "name=" ' + Frameid + ' "style=" position:absolute; top:-9999px; Left:-9999px "'; if (window. ActiveXObject) {if (typeof uri = = ' Boolean ') {iframehtml + = ' src= "' + ' javascript:false ' + ') ‘; } else if (typeof uri = = ' string ') {iframehtml + = ' src= "' + uri + '"; }} iframehtml + = '/> '; JQuery (iframehtml). AppendTo (Document.body); return JQuery (' # ' + Frameid). Get (0); }, Createuploadform:function (ID, Fileelementid, data) {//create frame var formId = ' juploadform ' + ID; var fileId = ' juploadfile ' + ID; var form = jQuery (' <form action= "" method= "POST" name= "' + formId + '" id= "' + formId + '" enctype= "Multipart/form-dat A "></form>"); if (data) { for (var i in data) {jQuery (' <input type= "hidden" name= "' + i + '" value= "' + data[i] + '"/> '). AppendTo (form); }} var oldelement = JQuery (' # ' + Fileelementid); var newelement = jQuery (oldelement). Clone (); JQuery (oldelement). attr (' id ', fileId); JQuery (Oldelement). before (newelement); JQuery (oldelement). AppendTo (form); Set properties for JQuery (form). CSS (' position ', ' absolute '); JQuery (Form). CSS (' top ', ' -1200px '); JQuery (Form). CSS (' left ', ' -1200px '); JQuery (Form). AppendTo (' body '); return form; }, Ajaxfileupload:function (s) {s = Jquery.extend ({}, jquery.ajaxsettings, s); var id = new Date (). GetTime () var form = Jquery.createuploadform (ID, S.fileelementid, (typeof (S.data) = = ' Undefine d '? False:s.data)); var io = jquery.createuploadiframe (ID, S.secureuri); var Frameid = ' juploadframe ' + ID; var formId = ' juploadform ' + ID; Monitoring requests if (S.global &&!jquery.active++) {JQuery.event.trigger ("Ajaxstart"); } var requestdone = false; Create the Request object var xml = {} if (S.global) JQuery.event.trigger ("Ajaxsend", [XML, S]); Wait for a response to return var uploadcallback = function (istimeout) {var io = document.getElementById (Frameid); try {if (Io.contentwindow) {xml.responsetext = Io.contentWindow.document.body? Io.contentWindow.document.body.innerHTML:null; Xml.responsexml = io.contentWindow.document.XMLDocument? Io.contentWindow.document.XMLDocument:io.contentWindow.document; } else if (io.contentdocument) {xml.responsetext = Io.contentDocument.document.body? io.contentdocume Nt.document.body.innerHTML:null; Xml.responsexml = io.contentDocument.document.XMLDocument? Io.contentDocument.document.XMLDocument:io.Contentdocument.document; }} catch (e) {Jquery.handleerror (s, XML, NULL, E); } if (XML | | istimeout = = "Timeout") {Requestdone = true; var status; try {status = istimeout! = "Timeout"? "Success": "Error"; Request succeeds if (Status! = "Error") {//process data var = jQuery . Uploadhttpdata (XML, S.datatype); if (s.success) s.success (data, status); Trigger Global return if (S.global) JQuery.event.trigger ("Ajaxsuccess", [XML, S]); } else Jquery.handleerror (S, XML, status); } catch (e) {status = "error"; Jquery.handleerror (S, XML, status, E); }//Request complete if (S.global) JQuery.event.trigger ("Ajaxcomplete", [XML, S]); Handle Global Ajax Counters if (S.global &&!--jquery.active) JQuery.event.trigger ("Ajaxstop "); Processing results if (s.complete) s.complete (XML, status); jquery (IO). Unbind () setTimeout (function () {try {jQuery (IO). rem Ove (); JQuery (Form). Remove (); } catch (E) {Jquery.handleerror (s, XML, NULL, E); }}, +) XML = null}}//timeout check if (s.timeout > 0) { SetTimeout (function () {if (!requestdone) uploadcallback ("timeout"); }, S.timeout); } try {var form = jQuery (' # ' + formId); JQuery (Form). attr (' action ', S.url); JQUery (Form). attr (' method ', ' POST '); JQuery (Form). attr (' target ', Frameid); if (form.encoding) {jQuery (form). attr (' Encoding ', ' multipart/form-data '); } else {jQuery (form). attr (' enctype ', ' multipart/form-data '); } jQuery (Form). Submit (); } catch (E) {Jquery.handleerror (s, XML, NULL, E); } jQuery (' # ' + Frameid). Load (uploadcallback); return {abort:function () {}}; }, Uploadhttpdata:function (r, type) {var data =!type; data = Type = = "xml" | | Data? R.responsexml:r.responsetext; if (type = = "Script") jquery.globaleval (data); if (type = = "json") {data = R.responsetext; var start = Data.indexof (">"); if (Start! =-1) {var end = Data.indexof ("<", start + 1); if (end! =-1) {data = data.substring (start + 1, end); }} eval ("data =" + data); data = Jquery.parsejson (jQuery (data). text ()); } if (type = = "html") jQuery ("<div>"). HTML (data). Evalscripts (); return data; }, 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). Trig GER ("Ajaxerror", [XHR, S, E]); } }})
Background Controller code
public class Fileuploadcontroller:controller {////GET:/fileupload/[HttpPost] Pub Lic Jsonresult Index () {StringBuilder info = new StringBuilder (); foreach (string file in Request.Files) {HttpPostedFileBase postfile = Request.files[file];//get Post file if (Postfile.contentlength = = 0) {continue; } String name = DateTime.Now.ToFileTime (). ToString (); String Fileexrensio = System.IO.Path.GetExtension (Request.files[file]. FileName). ToLower (); String newfilepath = System.Web.HttpContext.Current.Server.MapPath ("/upload/");//save path Postfile.save As (Newfilepath + name + fileexrensio);//save file info. Append ("/upload/" + name + Fileexrensio); } return Json (new {img_url = info). ToString ()}, "Text/html;charset=utf-8");//Make sure to format } }
Ajaxfileupload File upload <pre>,json string as null workaround