1

Please bear with me, my English is still improving... Below given code is what I am using to get an uploaded image saved in "UploadedImage" folder in ASP.NET MVC 5 project. But when I use my almost high-end PC having i7 processor, 16GB of RAM, 4GB Graphics Card, all works like a charm. But problem arises when I use my laptop having i3 processor and 8GB of ram or a PC having i3 processor having 4GB RAM.... What I get is only a black image.

public ActionResult Index(FormCollection form)
{ if (string.IsNullOrEmpty(form["image-data"])) return
    View();
var t = form["image-data"].Substring(form["image-data"].LastIndexOf(',') + 1);
byte[] bytes = Convert.FromBase64String(t);
string fileName = form["email"].Replace("@", "at");
fileName = fileName.Replace(fileName[fileName.LastIndexOf(".")].ToString(), "dot");
var fullPath = Path.Combine(Server.MapPath("~/UploadedImage/"), fileName + ".png");

Image image;

using (var streamBitmap = new MemoryStream(bytes))
{
    using (image = Image.FromStream(streamBitmap))
    {
        image.Save(fullPath);
        }
    }

Please help me, I could not get any help from anywhere, thanks in advance

  • I have used two more ways but to no avail: var t = form["image-data"].Substring(form["image-data"].LastIndexOf(',') + 1); byte[] imageBytes = Convert.FromBase64String(t); var randomFileName = Guid.NewGuid().ToString().Substring(0, 4) + ".png"; var filePath = Path.Combine(Server.MapPath("~/UploadedImage/"), randomFileName); System.IO.File.WriteAllBytes(filePath, imageBytes); – Nikhil Rastogi Jan 30 '23 at 07:17
  • It is already answered [here](https://stackoverflow.com/questions/25125127/asp-net-mvc-4-c-sharp-httppostedfilebase-how-do-i-store-file) – Manik Mar 08 '23 at 11:02

0 Answers0