想使用ajaxfileupload,发现使用json的话无法取得返回值,找了好多个帖子,终于解决掉: 页面代码: View Code
<input id="fileToUpload" type="file" name="fileToUpload" /> <input type="button" id="buttonUpload" onclick="return ajaxFileUpload();" value="Upload" /> <img id="loading" src="/Content/loading.gif" style="display: none;" /> js代码: View Code
function ajaxFileUpload() { $("#loading") .ajaxStart(function () { $(this).show(); }) .ajaxComplete(function () { $(this).hide(); }); $.ajaxFileUpload({ url: '/File/Upload', secureuri: false, fileElementId: 'fileToUpload', dataType: 'json', success: function (data) { alert(data.msg); }, error: function (data) { alert("error"); } }); return false; } FileController/Upload代码: View Code
[HttpPost] public JsonResult Upload() { string filename = string.Empty; if (Request.Files["fileToUpload"] != null) { filename = Request.Files["fileToUpload"].FileName; Stream st = Request.Files["fileToUpload"].InputStream; } return this.Json(new { err = "", msg = "xxx.jpg" }, "text/x-json"); }
其中重点就是: return Json(new { err = "", msg = "xxx.jpg" }, "text/x-json"); 需要给mvc中的json返回值指定contentType |
|
来自: ThinkTank_引擎 > 《综合》