简体   繁体   English

图像未使用asp.net显示

[英]Image is not displaying using asp.net

In the below code i have a image i have pass image url from codebehind but it is not displaying the image. 在下面的代码我有一个图像我有来自codebehind传递图像网址,但它没有显示图像。

public string strName = "SV";
        public string strFolder = "Documents";

 string Imgdocname = SearchDoc.DocumentName;
                string fileExt = System.IO.Path.GetExtension(Imgdocname);
                fileExt = fileExt.ToLower();
                if (fileExt == ".jpeg" || fileExt == ".jpg")
                {

                    docimg.ImageUrl  = "C:\\Search\\" + strName + "\\" + strFolder + "\\" + Imgdocname; //not working
                    docimg.Visible = true;
                 } else
                {docimg.ImageUrl  = "C:\\Search\\doc\\Documents\\image.jpeg";  //not working

                }

<div ID="imgContainer" runat="server" style="width: 700px; height: 300px;  overflow:auto; border: solid 1px black;
                    padding: 10px; margin-bottom: 5px;" >
<asp:Image ID="docimg" runat="server" />

          </div>

IS Search your Project Name? IS搜索您的项目名称?

If Yes then use 如果是,则使用

 if (fileExt == ".jpeg" || fileExt == ".jpg")
 {

           docimg.ImageUrl= "~/" + strName + "/" + strFolder + "/" + Imgdocname;
           docimg.Visible = true;
 }
 else
 {
          docimg.ImageUrl = "~/doc/Documents/image.jpeg";
          docimg.Visible=true;
 }   

As opposed to a client application, images in a web application are not included into the page directly. 与客户端应用程序相反,Web应用程序中的图像不会直接包含在页面中。 Instead, the page contains a link to the image resource that the browser requests after retrieving the HTML of the page. 相反,该页面包含浏览器在检索页面的HTML后请求的图像资源的链接。 Therefore you need to provide a URL that the client can retrieve. 因此,您需要提供客户端可以检索的URL。 In your sample you set the file path on the server as the image URL. 在您的示例中,您将服务器上的文件路径设置为图像URL。 This will not work as the client cannot access the file path on the server directly. 这不起作用,因为客户端无法直接访问服务器上的文件路径。

Instead you need to place the images in a folder that is accessible through the web server (eg IIS) so that you are able to set a URL like /img/picture.jpg that the client can use to retrieve the image. 相反,您需要将图像放在可通过Web服务器(例如IIS)访问的文件夹中,以便您能够设置客户端可用于检索图像的/img/picture.jpg URL。 The easiest way to achieve this is to place the images in a folder under the root folder of your application. 实现此目的的最简单方法是将图像放在应用程序根文件夹下的文件夹中。 In this case, you can set the image URL like this (assuming that the folder that the images are located in is named img ): 在这种情况下,您可以像这样设置图像URL(假设图像所在的文件夹名为img ):

var imgUrl = "~/img/";
if (fileExt == ".jpeg" || fileExt == ".jpg")
    imgUrl += strName + "/" + strFolder + "/" + Imgdocname;
else
    imgUrl += "doc/Documents/image.jpeg";
docimg.ImageUrl = imgUrl;
docimg.Visible = true;

The ~ is a placeholder for the root of your project on the server so that the link will work no matter if your application is installed at the root of a website or somewhere below. ~是服务器上项目根目录的占位符,这样无论您的应用程序是安装在网站的根目录还是下面的某个位置,链接都可以正常工作。

You cannot specify absolute path from any drive on computer. 您无法从计算机上的任何驱动器指定绝对路径。 The ImageUrl is not the computer's path but a Url path present in your project. ImageUrl不是计算机的路径,而是项目中存在的Url路径。

Make a folder Images in your project root directory and specify path as 在项目根目录中创建一个文件夹Images ,并将路径指定为

docimg.ImageUrl = "~/Images/image.jpeg";

The tilde ~ refers to root directory of project. 波浪号〜指的是项目的根目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM