C# ';HttpContext';不包含';当前';
我想将图像上载到图像文件夹,但它会显示此错误。前面是棱角7 Net核心MVC CustomerRepo.cs register.html-在角度应用程序中C# ';HttpContext';不包含';当前';,c#,asp.net-mvc,angular,asp.net-web-api,asp.net-core,C#,Asp.net Mvc,Angular,Asp.net Web Api,Asp.net Core,我想将图像上载到图像文件夹,但它会显示此错误。前面是棱角7 Net核心MVC CustomerRepo.cs register.html-在角度应用程序中 错误CS0117“HttpContext”在.NET Core中不包含“Current”的定义。上下文作为HttpContext属性是控制器类的一部分。请求可以作为http://www.HttpContext.Request获得,您可以从那里访问表单数据 但是,在内核中,文件上载的语法已经更改,因此如果没有一些更改,上面的代码将无法工作。在您
错误CS0117“HttpContext”在.NET Core中不包含“Current”的定义。上下文作为
HttpContext
属性是控制器类的一部分。请求可以作为http://www.HttpContext.Request获得,您可以从那里访问表单数据
但是,在内核中,文件上载的语法已经更改,因此如果没有一些更改,上面的代码将无法工作。在您的情况下,您使用的是模型绑定,您需要设置您的请求模型来处理传入的文件
以下内容来自上的Http上载文档:
[HttpPost(“上传文件”)]
公共异步任务发布(列表文件)
{
long size=files.Sum(f=>f.Length);
//临时位置中文件的完整路径
var filePath=Path.GetTempFileName();
foreach(文件中的var formFile)
{
如果(formFile.Length>0)
{
使用(var stream=newfilestream(filePath,FileMode.Create))
{
等待formFile.CopyToAsync(流);
}
}
}
//处理上载的文件
//未经验证,请勿依赖或信任FileName属性。
返回Ok(新的{count=files.count,size,filePath});
}
您可以对模型中的单个文件或作为参数传递[FromBody]的单个文件使用相同的接口
在您的情况下,传入模型应具有如下属性:
public-image{get;set;}
捕获入站文件。在Asp.Net Core
HttpContext
中不再具有该静态属性。这可能是一个错误。提供一份清单,澄清您的具体问题,或添加其他详细信息,以突出显示您的确切需求。正如目前编写的那样,很难准确地说出您在问什么。调用AddCustomer
的是什么/在哪里?这并不能回答问题
public bool AddCustomer(ExpressWebApi.Models.CustomerModel customer)
{
SqlConnection con =new SqlConnection();
con.ConnectionString="Data Source=.;Initial Catalog=BLAbLADB;Persist Security Info=True;User ID=sa;Password=sasasa";
SqlCommand cmd = new SqlCommand();
cmd.Connection=con;
//file upload start-----------------------
string imageName = null;
var httpRequest = HttpContext.Current.Request;
//upload the image
var postedFile = httpRequest.Files["cusImage"];
//Current custome filename
imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-");
imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName);
var filePath = HttpContext.Current.Server.MapPath("~/Images/" + imageName);
postedFile.SaveAs(filePath);
//file upload end-------------------------
cmd.CommandText=$@"INSERT INTO Customer ([CusName], [CusPassword], [CusEmail], [CusDob], [CusImage]) VALUES ('{customer.CusName}', '{customer.CusPassword}', '{customer.CusEmail}', '{customer.CusDob}','{imageName}');";
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
return true;
}
var customerData = {
"cusName": form.value.userName,
"cusPassword": form.value.password,
"cusEmail": form.value.email,
"cusDob": form.value.dob,
"cusImage" : this.fileToUpload
};
//for photo upload start---------------------------------
handleFileInput(file:FileList){
this.fileToUpload=file.item(0);
//show image preview
var reader = new FileReader();
reader.onload=(event:any)=>{
this.imageUrl=event.target.result;
}
reader.readAsDataURL(this.fileToUpload);
}
//for photo upload end---------------------------------