Home > Net >  Core call another upload dedicated controller in the method, such as old tip NullReferenceException
Core call another upload dedicated controller in the method, such as old tip NullReferenceException

Time:02-15

Wrote a special method of uploaded files in the control, the direct external call no problem, but in another controller calls this method suggests that the System. NullReferenceException: Object reference not set to an instance of an Object. mistake, say this line has a problem: var files=Request. Form. The files; , I do not know is to call in the right way, or what the reason, thank you please people copies, with two methods of code is as follows:

This is upload dedicated controller methods
 public async Task UploadFile () 
{
//Web root directory
String webRootPath=ConfigHelper. GetConfig (" ImgPath ");
Var date=Request;
Var files.=the Request Form. The files;
Long size=files. The Sum (f=& gt; F.L ength);
String shortTime=DateTime. Now. ToString (" yyyy/MM/dd ") + "/".
String filePhysicalPath=webRootPath + "/Content/GTP/made" + shortTime;//file path can be achieved by injecting IHostingEnvironment service object physical path of the root directory the root directory of the Web and content
. .
Using (var stream=new FileStream (filePhysicalPath + fileName, FileMode. Create))
{
Await the file. CopyToAsync (stream);
}
}
}
JObject job=new JObject ();
Job. The Add (new JProperty (" data ", jFile));
Return Ok (job. The ToString ());
}


Another controller is the method called such
 private UpFileController _upFileController; 
Public datacenterController (UpFileController UpFileController)
{
_upFileController=upFileController;
}


Public async Task AddChord2 ()
{



Var t=await _upFileController. UploadFile ();
Return Ok (t.T oStr ());






}

CodePudding user response:


The general is a class instance into a Null, will appear in this kind of circumstance


CodePudding user response:

Directly set a breakpoint debugging your own Request. The Form,
  •  Tags:  
  • C#
  • Related