Asked by:
upload image and save values

Question
-
User-1892044535 posted
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; } } }I need some help guys
Friday, March 1, 2019 9:53 AM
All replies
-
User475983607 posted
The string is not a valid Base64 string. Refactor your code a bit so you can see the results of the Replace() methods.
string temp = ScanData.Replace("data:;base64,", string.Empty); temp = temp.Replace("data:application/octet-stream;base64,", string.Empty); byte[] Data = Convert.FromBase64String(temp);
- What is the value of ScanData?
- What is the value after the first Replace()?
- What is the value after the second Replace()?
Friday, March 1, 2019 3:35 PM -
User-2054057000 posted
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.
Friday, March 1, 2019 3:46 PM -
User-1892044535 posted
yes mr.yoyogi
Saturday, March 2, 2019 9:25 AM -
User-943250815 posted
mas mas
byte[] Data = Convert.FromBase64String(ScanData.Replace("data:;base64,", string.Empty).Replace("data:application/octet-stream;base64,", string.Empty));
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_URIsAs already pointed split your string and check values in debug mode
Saturday, March 2, 2019 2:24 PM