分享

asp.net MVC3使用ajaxfileupload的json错误解决

 ThinkTank_引擎 2014-06-17

想使用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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约