如何在图像控件中显示图像从数据库和存储在文件夹中的图像的恢复路径 [英] how to show an image in image control retrived path from database and image stored in folder
问题描述
亲爱的先生,
这里我正在开发一个asp.net项目。我将图像路径存储在数据库中,并将图像存储在名为image的文件夹中。这里图像存储成功,但无法加载到图像控件中。
这是代码。
Dear sir,
Here I am working on a asp.net project. I stored my image path in database and image inside a folder named as image. here the image stored successfully but it could not load in an image control.
Here is the code.
if (!string.IsNullOrEmpty(Convert.ToString(dr["imagepath"])))
{
Image1.ImageUrl = Convert.ToString(dr["imagepath"]);
}
Plz帮我解决了这个问题。
谢谢&问候
Bigyan Ranjan Sahoo
Plz help me for shut out this problem.
Thanks & Regards
Bigyan Ranjan Sahoo
推荐答案
如果imagepath
列包含 c> 目录中文件的名称,然后它应该如下所示:
If theimagepath
column contains the name of a file inside theimages
directory, then it should be as simple as:
Image1.ImageUrl = "~/images/" + Convert.ToString(dr["imagepath"]);
如果列包含服务器上文件的物理路径,则会变得更复杂。假设所有内容都在同一物理文件夹结构中,您可能可以使用以下内容:
If the column contains the physical path of the file on the server, then it gets more complicated. Assuming everything is within the same physical folder structure, you might be able to use something like this:
string physicalPath = Convert.ToString(dr["imagepath"]);
string imagesPhysicalPath = Server.MapPath("~/images/");
if (physicalPath.StartsWith(imagesPhyisicalPath, StringComparison.OrdinalIgnoreCase))
{
string relativePath = physicalPath.Substring(imagesPhysicalPath.Length);
image.ImageUrl = "~/images/" + relativePath.Replace('\\', '/');
}
您可以使用Server.MapPath(路径)来获取正确的服务器路径。试试这段代码。
You can use Server.MapPath(path) to get correct server path. Try this code.
if (!string.IsNullOrEmpty(Convert.ToString(dr["imagepath"])))
{
Image1.ImageURL= Server.MapPath(dr["imagepath"].ToString());
}
这篇关于如何在图像控件中显示图像从数据库和存储在文件夹中的图像的恢复路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!