So here is the code i wrote for your saving images into your roots, you have to create a folder in your root of application for example upload
:
#region fileupload
string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string ret = Rename.ChangeName();
string SaveLocation = Server.MapPath("Upload") + "" + ret;
try
{
FileUpload1.PostedFile.SaveAs(SaveLocation);
}
catch (Exception ex)
{
if (ex is ArgumentNullException || ex is NullReferenceException)
{
throw ex;
}
}
string PicAddress = "~/Upload/" + ret;
#endregion
as you can see there is a method for change name, assuming you just want to save jpg files, for other files you can use extension:
public static string ChangeName()
{
return Guid.NewGuid().ToString("N") + ".jpg";
}
Simply after these code ran, you can easily save the PicAddress
as a string into your database, so make for example nvarchar field for it. anytime you want to show the image you just need to reference the address of the image to your image tag:
<img src="~/Upload/etcetc.jpg">
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…