【发布时间】:2019-09-10 14:01:18
【问题描述】:
我想上传图片到图片文件夹,但是会显示这个错误。前面是 Angular 7。
Asp.Net Core MVC
CustomerRepo.cs
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;
}
register.html - 在 Angular 应用程序中
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---------------------------------
错误 CS0117“HttpContext”不包含“当前”的定义
【问题讨论】:
-
在 Asp.Net Core 中
HttpContext不再具有该静态属性。 -
这可能是XY problem。提供minimal reproducible example 以阐明您的特定问题或添加其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
-
什么/在哪里调用
AddCustomer?
标签: c# asp.net-mvc angular asp.net-web-api asp.net-core