为什么我的JSON结果方法返回响应错误(500)

时间:2015-08-18 11:52:12

标签: asp.net-mvc angularjs httpresponse

我在我的ASP.net MVC应用程序中执行AJAX文件上传操作,其中我也使用angularjs。

服务方法致电:

    uploadFile: function() {
        var defer = $q.defer();
        var fileInput = document.getElementById("files");
        var messageHeaders = { 'Content-Type': 'application/x-www-form-urlencoded' };
        messageHeaders['X-File-Name'] = encodeURI(fileInput.files[0].name);
        messageHeaders['X-File-Type'] = encodeURI(fileInput.files[0].type);
        var fileData = fileInput.files[0];

        $http({
            url: window.ROOT + 'EmployerBenefit/UploadFile',
            method: "POST",
            data: fileData,
            headers: messageHeaders
        }).success(function(callback) {
            defer.resolve(callback);
        });
        return defer.promise;
    },

控制器方法:

[AllowAnonymous]
public JsonResult UploadFile(string attachmentType, int planId = 24527)
{
    var success = true;
    string message = ApplicationMessage.InvalidParameter;
    // Validation part removed
    if (success)
    {
        var inputStream = Request.InputStream;
        var fileLenght = (int) inputStream.Length;
        var fileName = HttpUtility.UrlDecode(Request.Headers["X-File-Name"]);
        var bytes = new byte[fileLenght];
        Request.InputStream.Read(bytes, 0, fileLenght);
        if (!Directory.Exists(Server.MapPath("/Content/Images/" + CommonFunctions.ParseString(planId))))
        {
            Directory.CreateDirectory(Server.MapPath("/Content/Images/" + CommonFunctions.ParseString(planId)));
        }
        System.IO.File.WriteAllBytes(
            Server.MapPath("/Content/Images/" + CommonFunctions.ParseString(planId) + "/" + fileName), bytes);

    }

    Response.StatusCode = (int)HttpStatusCode.OK;
    return Json(new {success, message}, JsonRequestBehavior.AllowGet);
}

问题:

我有一个拦截器来处理工厂中的响应错误,即使我将响应状态设置为OK,拦截器正在拾取responseError(500)?

我哪里错了?

1 个答案:

答案 0 :(得分:0)

我将内容类型更改为undefined,这似乎有效。

{ 'Content-Type': undefined };