I am trying to upload an image path into database and save file in the images folder. This works correctly except for the getExtension part (.jpg, .gif etc…) the Path.GetExtension method is always returning null (“”) and I don’t have any idea why.
Below you can find the code used:
string imagesFolderPath = Server.MapPath("//Images");
imagesFolderPath += "//";
string newImageName = addProduct.id.ToString();
string extension = Path.GetExtension(fileUpload.FileName.ToString());
addProduct.imagePath = "Images//" + newImageName + extension;
fileUpload.SaveAs(imagesFolderPath + newImageName);
Per the MSDN Documentation:
The only way you can get an empty string,
"", is for the path to not benulland the path to not have an extension.You’re getting an empty string because the path doesn’t have an extension.