为什么我的JSON结果方法返回响应错误(500)?
为什么我的JSON结果方法返回响应错误(500)?
这个问题已经有了答案:
我在我的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,拦截器也会捕获响应错误(500)
我错在哪里了?
admin 更改状态以发布 2023年5月22日