C# 如何在C中使用imageurl显示图像#
UploadeImage是图像控制的ID。imagepath的路径是E:\Folder1\Folder2\Folder3\Images\1.png 正在保存图像,但我无法显示上载的图像。我需要在这行中添加任何显示图像的内容吗C# 如何在C中使用imageurl显示图像#,c#,asp.net,C#,Asp.net,UploadeImage是图像控制的ID。imagepath的路径是E:\Folder1\Folder2\Folder3\Images\1.png 正在保存图像,但我无法显示上载的图像。我需要在这行中添加任何显示图像的内容吗 if (FileUpload1.HasFile) { string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); //string
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();
}
图像中的图像Url应如下所示
uploadedimage.ImageUrl = Server.MapPath(@"~/"+imagepath);
uploadedimage.DataBind();
尝试删除服务器。映射路径尝试此操作
在根目录中创建数据文件夹
"~/"+ imagepath
在iis上托管网站或其他东西,不能以这种方式工作。在这方面有一些概念需要学习,但最好从了解什么是虚拟目录开始 页面上的一句话: 在IIS 7中,每个应用程序都必须有一个虚拟目录,称为 根虚拟目录,并将应用程序映射到物理目录 包含应用程序内容的目录 这意味着这是一个应用程序“内容”所在的目录;它可以是简单的文本文件、图像等,也可以是复杂的服务器端页面,如aspx,甚至是经典的asp或php等。现在,托管web应用程序无法访问此目录以外的任何内容 因此,您打算共享的路径不是这样工作的。有几个选项可以处理这种情况
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string imagepath =Server.MapPath("~/Data/"+FileName);
FileUpload1.SaveAs(imagepath);
uploadedimage.ImageUrl="~/"+imagepath;
}
uploadedImage.ImageUrl = "~/MyImageHandler.ashx?filename=foo.png" //in ur server code.
在处理程序中,您可以编写如下内容:
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string imagepath =Server.MapPath("~/Data/"+FileName);
FileUpload1.SaveAs(imagepath);
uploadedimage.ImageUrl="~/"+imagepath;
}
uploadedImage.ImageUrl = "~/MyImageHandler.ashx?filename=foo.png" //in ur server code.
我自己没有使用过这种方法,但我相信您应该将数据绑定到实际的图像,而不是imagepath中的路径(请参见),图像将被映射…让我使用这些代码并检查是否得到了预期的结果。谢谢@deocwalk