简体   繁体   English

如何在C#中使用imageurl显示图像

[英]how to display image using imageurl in C#

if (FileUpload1.HasFile)
        {
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            //string path = Server.MapPath(@"~\\"+Session["parentfolder"].ToString() +"\\"+ Session["brandname"].ToString() + "\\" + Seasonfolders.SelectedItem.Text + "\\" + stylefolders.SelectedItem.Text + "\\Images\\" + FileName);
            string root = Server.MapPath("~");
            string path = Path.GetDirectoryName(root);
            string path1 = Path.GetDirectoryName(path);
            string rootfolder = Path.GetDirectoryName(path1);
            string imagepath = rootfolder + Session["brandname"].ToString() + "\\" + Seasonfolders.SelectedItem.Text + "\\" + stylefolders.SelectedItem.Text + "\\Images\\" + FileName;
            FileUpload1.SaveAs(imagepath);
            //objBAL.SaveImage("Image", Session["brandname"].ToString(), Seasonfolders.SelectedItem.Text, stylefolders.SelectedItem.Text, FileName, imagepath, Convert.ToInt32(Session["Empcode"]));
            uploadedimage.ImageUrl = Server.MapPath(@"~/"+imagepath);
            uploadedimage.DataBind();

        }

uploadedimage is ID for Image control. uploadedimage是Image控件的ID。 The path of imagepath is E:\\Folder1\\Folder2\\Folder3\\Images\\1.png imagepath的路径是E:\\ Folder1 \\ Folder2 \\ Folder3 \\ Images \\ 1.png

The image is getting saved but I cannot able to display the uploaded image. 图像正在保存,但我无法显示上传的图像。 Do I need to add anything in this line which is to display an image .. 我是否需要在此行中添加任何显示图像的内容..

            uploadedimage.ImageUrl = Server.MapPath(@"~/"+imagepath);
            uploadedimage.DataBind();

The image Url in your image should be like this 您图片中的图片网址应如下所示

"~/"+ imagepath

Try removing Server.MapPath 尝试删除Server.MapPath

Try this 尝试这个

Create Data Folder in your root directory 在根目录中创建数据文件夹

 if (FileUpload1.HasFile)
    {
        string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);              
        string imagepath =Server.MapPath("~/Data/"+FileName);
        FileUpload1.SaveAs(imagepath); 

        uploadedimage.ImageUrl="~/"+imagepath;
    }

Hosting websites or stuff on iis, does not work this way. 托管网站或iis上的东西,不会这样。 There are a few concepts one needs to learn on that front, but the best place to start with is understand what is virtual directory . 在这方面需要学习一些概念,但最好的开始是理解什么是虚拟目录

One quote from the this page: 从一个引用页面:

In IIS 7, each application must have a virtual directory, known as the root virtual directory, and maps the application to the physical directory that contains the application's content 在IIS 7中,每个应用程序必须具有一个虚拟目录,称为根虚拟目录,并将应用程序映射到包含应用程序内容的物理目录

So it means this is a directory where the application's "content" reside; 所以这意味着这是应用程序的“内容”所在的目录; it could be simple text files, images, etc, to complex server side pages like aspx or even classic asp, or php, etc. Now anything out of this directory is not accessible by the hosted web application. 它可能是简单的文本文件,图像等,复杂的服务器端页面,如aspx甚至经典的asp或php等。现在托管的Web应用程序无法访问此目录之外的任何内容。

Hence the path you intend to share doesn't work that way. 因此,您打算分享的路径不会那样。 There are a few options to handle this scenario. 有几种方法可以处理这种情况。

  1. In iis you could create a sub-virtual directory, and map its path to where your images are stored to the physical location where the images reside. 在iis中,您可以创建一个子虚拟目录,并将其路径映射到图像存储的位置到图像所在的物理位置。

  2. Provided your web application (when hosted on iis) has access to the path where the images reside, you can write code to read the file, and send the stream of bytes back so that your web page can render image properly. 如果您的Web应用程序(在iis上托管时)可以访问映像所在的路径,则可以编写代码来读取文件,然后发回字节流,以便您的Web页面可以正确呈现图像。

2nd approach is usually implemented by a handler (ashx) via which you can pass the image name as query string argument, and return the bytes. 第二种方法通常由处理程序(ashx)实现,您可以通过该处理程序将图像名称作为查询字符串参数传递,并返回字节。 Hence in short you do something like this: 因此,简而言之,你做这样的事情:

uploadedImage.ImageUrl = "~/MyImageHandler.ashx?filename=foo.png" //in ur server code.

In the handler you write something like this: 在处理程序中,你写这样的东西:

public class MyImageHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
    // Comment out these lines first:
    // context.Response.ContentType = "text/plain";
    // context.Response.Write("Hello World");

    context.Response.ContentType = "image/png";

    var filepath = @"E:\your_image_dir\" + Request.QueryString["filename"];

    //Ensure you have permissions else the below line will throw exception.
    context.Response.WriteFile(filepath);

    }

    public bool IsReusable {
    get {
        return false;
    }
    }
}

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

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