C# 将图像保存到服务器
您好,我目前可以使用此功能将图像保存到我的服务器:C# 将图像保存到服务器,c#,asp.net,file-upload,C#,Asp.net,File Upload,您好,我目前可以使用此功能将图像保存到我的服务器: HttpPostedFile filePosted = CategoryImage.PostedFile; string fullPath = ""; if (filePosted != null && filePosted.ContentLength > 0) { int contentLength = Ca
HttpPostedFile filePosted = CategoryImage.PostedFile;
string fullPath = "";
if (filePosted != null && filePosted.ContentLength > 0)
{
int contentLength = CategoryImage.PostedFile.ContentLength;
string contentType = CategoryImage.PostedFile.ContentType;
string filename = filePosted.FileName;
string fileextension = Path.GetExtension(filename);
fullPath = Server.MapPath(Path.Combine("~/img/logos/", filename));
if (fileextension == ".jpg" || fileextension == ".jpeg" || fileextension == ".png" || fileextension == ".bmp")
{
CategoryImage.PostedFile.SaveAs(fullPath);
}
}
但我的数据库中的图像路径保存为:
D:\Projects\*************\****\img\logos\image1.png
现在,当我将此路径指定给html img“src”属性时,图像不会显示。我在运行代码时没有错误,我更希望路径如下:
~/******/img/image1.png
当我使用上述格式时,会显示图像。Server.MapPath(“~”)返回应用程序根目录的物理路径
您是否尝试过使用fullPath=Server.MapPath(Path.Combine(“/img/logos/”,filename))
Server.MapPath(“/”)返回域名根目录的物理路径(不一定与应用程序根目录相同)
我认为使用“/”可以将图像放置在IIS可以访问的文件夹中,而不是D:文件夹中。您可以用相对路径替换完整路径,并将其插入src属性中
string s1 = @"D:\Projects\*************\****\img\logos\image1.png";
string s2 = "~/****/" + s1.Substring(s1.IndexOf("img")).Replace("\\", "/");
// use s2 as src attribute for img