Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
down vote
favorite
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 string so SaveAs giving an error.

why I am using string? because when I am using public httppostfilebase image file {get;set;} show null value in controller .

imagePath: this variable contains string bytes image file: this variable contains the name of the image

I am not getting any error but the 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 

Any expert here to resolve my issue, and tell me what is the problem in my code.


What I have tried:

Model

public string imagePath { get;set; }
public string imageFile { get;set; }
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);
Posted
Updated 15-Oct-18 8:24am
v2
Comments
F-ES Sitecore 16-Oct-18 8:45am    
See below for how to convert the base64 string into an image

https://stackoverflow.com/questions/18827081/c-sharp-base64-string-to-jpeg-image

1 solution

Put your code into a try/catch block and evaluate any exception that might be thrown. My guess is that maybe the AppFiles\Images path does not exist, or that the filename is somehow invalid, or maybe that it already exists in the specified path. Some exception handling on your part will either show you the problem, or eliminate the file system as the problem.
 
Share this answer
 
Comments
Member 13186133 15-Oct-18 16:01pm    
Sir kindly read my description. I don't have any error my problem is I am using string type due to string type I cannot use SaveAs kindly tell me the solution if the image file is string How to Use SaveAs, and SaveAs work with Httppostfilebase but I am not using this. read my description
#realJSOP 16-Oct-18 5:34am    
"Save as not working" doesn't tell us ANYTHING. I repeat - put a try/catch block around your code.
Member 13186133 16-Oct-18 16:26pm    
Sir, I am sorry but your guess is totally wrong, I think you didn't get my point what I am saying.
#realJSOP 16-Oct-18 16:46pm    
Whatever.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900