Step 1: Add following method in your web api controller class:
[AllowAnonymous]
[Route("SaveImage")]
public async Task<IHttpActionResult> SaveImage(ImageModel image)
{
SweepsContext context = new SweepsContext();
context.Images.Add(new Image
{
MimeType = image.MimeType,
Data = Convert.FromBase64String(image.ImageString),
Name = image.Name
});
context.SaveChanges();
return Ok();
}
Step 2: Create ImageModel named model in your Models folder as following.
public class ImageModel
{
public string Name { get; set; }
public string MimeType { get; set; }
public string ImageString { get; set; }
}
Step 3: Create Image class in your entities folder or where ever you are saving your code first entities.
public class Image
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string MimeType { get; set; }
public byte[] Data { get; set; }
}
Step 4: Add following action in MVC controller.
public ActionResult Image(int id)
{
YourContext context = new YourContext();
var image = context.Images.FirstOrDefault(x => x.Id == id);
return new FileContentResult(image.Data, image.MimeType);
}
[AllowAnonymous]
[Route("SaveImage")]
public async Task<IHttpActionResult> SaveImage(ImageModel image)
{
SweepsContext context = new SweepsContext();
context.Images.Add(new Image
{
MimeType = image.MimeType,
Data = Convert.FromBase64String(image.ImageString),
Name = image.Name
});
context.SaveChanges();
return Ok();
}
Step 2: Create ImageModel named model in your Models folder as following.
public class ImageModel
{
public string Name { get; set; }
public string MimeType { get; set; }
public string ImageString { get; set; }
}
Step 3: Create Image class in your entities folder or where ever you are saving your code first entities.
public class Image
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string MimeType { get; set; }
public byte[] Data { get; set; }
}
Step 4: Add following action in MVC controller.
public ActionResult Image(int id)
{
YourContext context = new YourContext();
var image = context.Images.FirstOrDefault(x => x.Id == id);
return new FileContentResult(image.Data, image.MimeType);
}
No comments:
Post a Comment