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);
tereško
  • 58,060
  • 25
  • 98
  • 150
  • Are you getting any error? – Tasos K. Oct 15 '18 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. – Journal Trends Oct 15 '18 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 '18 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 – Journal Trends Oct 16 '18 at 09: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. – Reinstate Monica Cellio Oct 16 '18 at 09:43

1 Answers1

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);
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • My problem is this line Quot[i].imageFile.SaveAs(fileName); SaveAs show error How to resolve this .@CodeCaster – Journal Trends Oct 16 '18 at 09:44
  • and this my complete [link](https://stackoverflow.com/questions/52756428/how-to-insert-image-on-client-side-using-jquery-ajax) you can see what is the problem in my code if i am using Httppostfilebase – Journal Trends Oct 16 '18 at 09:48
  • See the link in my answer, your form is not OK. – CodeCaster Oct 16 '18 at 09: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'.' – Journal Trends Oct 16 '18 at 09:57
  • can you tell me what is the problem in my from @CodeCaster – Journal Trends Oct 16 '18 at 09:58
  • where I use this below the SaveAs File.WriteAllBytes(fileName, data); but getting error i have share the error @CodeCaster – Journal Trends Oct 16 '18 at 14:54