Controller
[HttpPost]
public JsonResult Slider_BannerEkle(ResimlerViewModel rsm)
{
ANDWECOMResimler resimlerViewModel = new ANDWECOMResimler
{
IMGFILE = rsm.IMGFILE,
IMGNAME = rsm.IMGNAME,
IMGORDER = rsm.IMGORDER,
IMGSTATE = rsm.IMGSTATE,
IMGURL = rsm.IMGURL
};
_uof.ResimRepository.Add(resimlerViewModel);
_uof.Save();
return Json(rsm);
}
View Model
public class ResimlerViewModel
{
[Key]
public Int16 IMGID { get; set; }
public string IMGNAME { get; set; }
public string IMGURL { get; set; }
public Int16 IMGORDER { get; set; }
public bool IMGSTATE { get; set; }
public string IMGFILE { get; set; }
public IFormFile file { get; set; }
}
I'm trying to convert images to base64 but it doesn't work.I tried a few times on the controller side, but I can't use the codes I use in mvc here.
CodePudding user response:
Controller:
[HttpPost]
public string getFile(UserModel user) {
var ms = new MemoryStream();
user.file.CopyTo(ms);
var fileBytes = ms.ToArray();//to byte array
string s = Convert.ToBase64String(fileBytes);//to base64string
return s;
}
View:
@model UserModel
<form enctype="multipart/form-data" method="post" asp-controller="Home" asp-action="getFile">
<input asp-for="name" type="text" />
<input asp-for="file" type="file">
<input type="submit"/>
</form>
Model:
public class UserModel
{
public string name { get; set; }
public IFormFile file { get; set; }
}