$.ajax({
url: "/Home/ReadText",
type: "POST",
cache: false,
async: false,
data: function () {
var data = new FormData();
data.append("file", jQuery("#file").get(0).files[0]);
data.append("selected", drop);
return data;
}(),
contentType: false,
processData: false,
success: function (response) {
debugger;
$("#target").empty();
$("#target").load("/Home/Results");
},
error: function (jqXHR, textStatus, errorMessage) {
console.log(errorMessage);
}
});
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。
[HttpPost]
public ActionResult ReadText(HttpPostedFileBase file, string selected)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
Session["filename"] = fileName.Replace(".txt","");
string content = file.InputStream.ToString();
path = Path.Combine(Server.MapPath("~/UploadedImages/"), fileName);
file.SaveAs(path);
}
string texts = System.IO.File.ReadAllText(path);
texts = texts.Replace(","," ");
string ofile = "names.txt";
string Spellpath= Path.Combine(Server.MapPath("~/MegTrans/"), ofile);
TextWriter tw1 = new StreamWriter(Spellpath, false, Encoding.Default);
tw1.WriteLine(texts);
tw1.Close();
Session["score"] = texts;
return PartialView("Results");
}
我从Chrome中的开发模式获得了异常的堆栈跟踪。
[NullReferenceException: Object reference not set to an instance of an object.]
Tesseract_MVC_Test.Controllers.HomeController.ReadText(HttpPostedFileBase file, String selected) +1675
lambda_method(Closure , ControllerBase , Object[] ) +190
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +228
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +34
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +38
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +68
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +71
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +386
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +69
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +38
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +186
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +59
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +67
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +44
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +67
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +399
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +157