0

I want to save image path in the database. I am using string type image not save in folder but image name also saved in the database. I am using a string so SaveAs() doesn't compile.

Why I am using string? Because when I am using public HttpPostedFileBase imagefile {get;set;} it shows a null value in the controller.

Model

   public string imagePath { get;set; } // contains the name of the image
   public string imageFile { get;set; } // contains string bytes 

C#

byte[] data = Convert.FromBase64String(Quot[i].imageFile.Replace("data:image/jpeg;base64,", ""));

Image img;
MemoryStream ms = new MemoryStream(data, 0, data.Length);
ms.Write(data, 0, data.Length);
img = Image.FromStream(ms, true);


string fileName = Path.GetFileNameWithoutExtension(Quot[i].imagePath);
string extension = Path.GetExtension(Quot[i].imagePath);
fileName = fileName + DateTime.Now.ToString("dd/MM/yyyy") + extension;
Quot[i].imagePath = "~/AppFiles/Images/" + fileName;
fileName = Path.Combine(HttpContext.Current.Server.MapPath("~/AppFiles/Images/"), fileName);
Quot[i].imageFile.SaveAs(fileName);
5
  • Are you getting any error?
    – Tasos K.
    Oct 15, 2018 at 15:48
  • No. problem in the last line of code how to save in a folder. i am using string but SaveAs not working save as work only Httppostfilebase. in this scenario How to save .@Tasos K. Oct 15, 2018 at 17:06
  • Is there a specific reason on why you load the file contents to an Image? If you don't wish to process the image, you could handle it as any other file.
    – Tasos K.
    Oct 15, 2018 at 23:10
  • Sir kindly tell me how to save the image in Folder SaveAs not working when I use string. How to resolve this issue. if this is done my problem resolved. @Tasos K Oct 16, 2018 at 9:34
  • See the answer from @CodeCaster below. Fix the actual problem, not the new one you've found when trying to ignore the real issue. Oct 16, 2018 at 9:43

1 Answer 1

0

Why I am using string? Because when I am using public HttpPostedFileBase imagefile {get;set;} it shows a null value in the controller.

Then rather fix that problem, instead of converting your code to work with a base64 string.

See for example HTTPPostedFileBase always return null in controller.

Anyhow, if you want to save an image, of which byte[] data holds the bytes, then simply write those bytes to a file:

File.WriteAllBytes(fileName, data);
6
  • My problem is this line Quot[i].imageFile.SaveAs(fileName); SaveAs show error How to resolve this .@CodeCaster Oct 16, 2018 at 9:44
  • and this my complete link you can see what is the problem in my code if i am using Httppostfilebase Oct 16, 2018 at 9:48
  • See the link in my answer, your form is not OK.
    – CodeCaster
    Oct 16, 2018 at 9:52
  • when i replace this line show this error: : 'Could not find a part of the path 'D:\Office Project\New\InventoryManagementSystem\InventoryManagementSystem\AppFiles\Images\elementary16\10\2018.jpg'.' Oct 16, 2018 at 9:57
  • can you tell me what is the problem in my from @CodeCaster Oct 16, 2018 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.