我正在尝试将包含Angularjs客户端图像的表单数据发布到Web API后端,但是会收到错误: "无法创建System.Web.HttpPostedFileBase类型的实例。 Type是接口或抽象类,无法实例化。路径' ProfileImage',第1行,第299位。"
我的角度代码是
$scope.RegisterUser = function () {
$http({
method: 'POST',
url: 'http://localhost:2434/api/Account/BrandRegistration/',
data: $scope.brandForm,
file : $scope.brandForm.ProfileImage
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log("libin");
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
我的网络API方法
public async Task<IHttpActionResult> PostBrandRegistration(BrandRegistration brandVM)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
if (!roleManager.RoleExists("brand"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "brand";
roleManager.Create(role);
}
if (!roleManager.RoleExists("influencer"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "influencer";
roleManager.Create(role);
}
var user = new ApplicationUser()
{
UserName = brandVM.Email,
Email = brandVM.Email
};
var fileName = "";
var file = HttpContext.Current.Request.Files.Count > 0 ?
HttpContext.Current.Request.Files[0] : null;
if (file != null && file.ContentLength > 0)
{
fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(
HttpContext.Current.Server.MapPath("~/App_Data/ProfileImage"),
fileName
);
file.SaveAs(path);
}
user.BrandUser = new BrandUser()
{
FullName = brandVM.FullName,
ContentType = brandVM.ContentType,
Description = brandVM.Description,
URL = brandVM.URL,
ContactPerson = brandVM.ContactPerson,
Position = brandVM.Position,
PhoneNumber = brandVM.PhoneNumber,
ContactEmail = brandVM.Email,
Address = brandVM.Address,
MarketPlace = brandVM.MarketPlace,
Campaigns = brandVM.Campaigns,
InfluencerRating = brandVM.InfluencerRating,
ProfileImage = fileName
};
user.BankDetail = new BankDetail()
{
AccountNumber = brandVM.AccountNumber,
AccountName = brandVM.AccountNumber,
IRD = brandVM.IRD,
GST = brandVM.GST
};
IdentityResult result = await UserManager.CreateAsync(user, brandVM.Password);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
else
{
await this.UserManager.AddToRoleAsync(user.Id, "brand");
return Ok();
}
}
我的视图模型是
public class BrandRegistration
{
public string Email { get; set; }
public string Password { get; set; }
public string PasswordConfirmation { get; set; }
public string FullName { get; set; }
public string ContentType { get; set; }
public HttpPostedFileBase ProfileImage { get; set; }
public string Description { get; set; }
public string URL { get; set; }
public string ContactPerson { get; set; }
public string Position { get; set; }
public string Company { get; set; }
public int PhoneNumber { get; set; }
public string ContactEmail { get; set; }
public string Address { get; set; }
public string AccountNumber { get; set; }
public string AccountName { get; set; }
public string IRD { get; set; }
public string GST { get; set; }
public bool MarketPlace { get; set; }
public bool Terms { get; set; }
public double InfluencerRating { get; set; }
public int Campaigns { get; set; }
}
如果有人能告诉我哪里出错了,我真的很感激。
答案 0 :(得分:0)
我不熟悉.NET,但您肯定应该在客户端使用表单数据。
var fd = new FormData();
fd.append('file', $scope.brandForm.ProfileImage);
fd.append('data', $scope.brandForm)
$http({
method: 'POST',
url: 'http://localhost:2434/api/Account/BrandRegistration/',
data: fd
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})