Hello guys, I try to save data with pictures and without pictures but I do not know where the mistake He gives me this
here is code for controller
public ActionResult Scan()
{
string ScanData;
// blob(image) coming in
using (Stream receiveStream = Request.InputStream)
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
// Reading blob(image)
ScanData = readStream.ReadToEnd();
}
}
// Removing extras from blob(image)
byte[] Data = Convert.FromBase64String(ScanData.Replace("data:;base64,", string.Empty).Replace("data:application/octet-stream;base64,", string.Empty));
//blob(image) Temporary held until the rest of data comes in to "Create" method.
TempData["ScannedImage"] = Data;
//System.IO.File.WriteAllBytes("FileName.png", Data); // save scanned images to files.
return null; // return nothing, the rest of the data will be processed in the "Create" method.
}
and this a create action
public async Task<ActionResult> Create(Inbox model/*,IEnumerable<HttpPostedFileBase>File*/)
{
var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());
if (ModelState.IsValid)
{
model.User = currentUser;
var max = new Inbox();
max.File = TempData["ScannedImage"] as byte[];//from "Scan" method, converted back to byte[]
max.NameWared = model.NameWared;
db.Inboxs.Add(model);
db.SaveChanges();
string url = Url.Action("List");
return Json(new { success = true, url = url });
}
return View(model);
}
and this is my model
namespace Arcive_project.Models
{
public class Inbox
{
[Key]
public int InboxId { get; set; }
public string WaridNO { get; set; }
public string NameWared { get; set; }
public byte[] File { get; set; }
[NotMapped] // Won't be mapped to db, only used to show images on page.
public string image { get; set; }
}
}
You basically store images on some directory of the server and store only the image path on the database.
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
9 Points
33 Posts
upload image and save values
Mar 01, 2019 09:53 AM|mas mas|LINK
Hello guys, I try to save data with pictures and without pictures but I do not know where the mistake He gives me this
here is code for controller
and this a create action
and this is my model
I need some help guys
All-Star
52101 Points
23237 Posts
Re: upload image and save values
Mar 01, 2019 03:35 PM|mgebhard|LINK
The string is not a valid Base64 string. Refactor your code a bit so you can see the results of the Replace() methods.
Participant
1253 Points
926 Posts
Re: upload image and save values
Mar 01, 2019 03:46 PM|yogyogi|LINK
Saving images in database is a wrong approach.
You basically store images on some directory of the server and store only the image path on the database.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
9 Points
33 Posts
Re: upload image and save values
Mar 02, 2019 09:25 AM|mas mas|LINK
yes mr.yoyogi
Participant
1051 Points
658 Posts
Re: upload image and save values
Mar 02, 2019 02:24 PM|jzero|LINK
If you have to replace data like above looks like image is coming from an http stream where image is inline.
If yes the "data:;base64," should include media type like "data:image/gif;base64," or "data:image/jpeg;base64,"
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
As already pointed split your string and check values in debug mode