Guest User

Guest User

  • Tech Writer
  • 611
  • 127.9k

MultipartForm Data in .net core.

Jul 24 2020 1:35 AM
It's me code i have store data in data base with multipart form .It's api run suessfully on local but not run live (Throw internal server error).Please help me .It's more important for me. 
 
public async Task<UploadFileResult> UploadProfileAsync([FromForm]UploadProfile profile)
{
UploadFileResult obj_result = new UploadFileResult();
UserProfileMaster obj_user = new UserProfileMaster();
try
{
var user =_context.UserProfileMaster.Where(x => x.UserId ==Convert.ToInt32(profile.UserId)).FirstOrDefault();
if (user == null)
{
string filePath = null;
var wwwrootPath = webHost.ContentRootPath + "\\uploads\\";
string thisFileName = Path.Combine(profile.File.FileName);
if (profile.File?.Length > 0)
{
filePath = Path.Combine(wwwrootPath, profile.File.FileName);
using FileStream fileStream = new FileStream(filePath, FileMode.Create);
await profile.File.CopyToAsync(fileStream);
}
obj_user.UserId =Convert.ToInt32(profile.UserId);
obj_user.FirstName = profile.FirstName;
obj_user.Address = profile.Address;
obj_user.MobileNumber = profile.MobileNumber;
obj_user.Country = profile.Country;
obj_user.LevelBaseBall = profile.LevelBaseBall;
obj_user.ZipCode = profile.ZipCode;
obj_user.InstaUserName = profile.InstaUserName;
obj_user.UserImage = thisFileName;
obj_user.UpdateDate = DateTime.UtcNow;
_context.UserProfileMaster.Add(obj_user);
_context.SaveChanges();
obj_result.Response = 1;
obj_result.ErrorMessage = "Profile was successfully uploaded";
obj_result.FileName = thisFileName;
UpdateProfileData objdata =new UpdateProfileData();
objdata.UserId = profile.UserId;
objdata.UserName = profile.FirstName;
objdata.MobileNumber = profile.MobileNumber;
objdata.LevelBaseBall = profile.LevelBaseBall;
objdata.ZipCode = profile.ZipCode;
objdata.InstaUserName = profile.InstaUserName;
objdata.Address = profile.Address;
objdata.Country = profile.Country;
obj_result.UserData = objdata;
}
else
{
obj_result.Response = 2;
obj_result.ErrorMessage = "User Already Exixts";
}
}
catch (Exception ex)
{
obj_result.Response = 0;
obj_result.ErrorMessage = ex.InnerException.ToString();
}
return obj_result;
}
 
 
It's me model code
public class UploadProfile
{
public string UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MobileNumber { get; set; }
public string Country { get; set; }
public string LevelBaseBall { get; set; }
public string ZipCode { get; set; }
public string InstaUserName { get; set; }
public string Address { get; set; }
public IFormFile File { get; set; }
//[Column(TypeName = "date")]
//public string UserImage { get; set; }
}
 

Answers (1)