【问题标题】:Trying to show the image in website after Choosing it and then saving same in db选择后尝试在网站中显示图像,然后将其保存在数据库中
【发布时间】:2017-10-18 09:42:33
【问题描述】:

我试图在选择图像然后将其保存到数据库后在网站上显示图像。 C# .aspx

<td ><strong>Product Photograph</strong></td>
         <td ><asp:FileUpload runat="server" ID="FileUpload1"/>
             <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />             
             <asp:Image ID="Image1" runat="server" />
         </td>

c#

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
            List<ListItem> files = new List<ListItem>();
            foreach (string filePath in filePaths)
            {
                string fileName = Path.GetFileName(filePath);
                files.Add(new ListItem(fileName, "~/Images/" + fileName));
            }}
}

我不知道如何附加选择的 ID="Image1" 图像。任何帮助将不胜感激。

【问题讨论】:

标签: c# asp.net


【解决方案1】:

这可以帮助您从文件中读取图像并在 asp:Image 控件中绑定

 Image1.ImageUrl= "data:image/jpg;base64," + Convert.ToBase64String(File.ReadAllBytes(fileName));

【讨论】:

  • 我收到此错误“找不到文件 'C:\Program Files (x86)\IIS Express\IMG_20171003_164329.jpg'。
  • 首先检查文件名。可能你的文件名路径无效。因为我在这里测试过,没问题。谢谢
  • 您好,我的文件位置在“E:\pc\Projects\TDC\TDC_Project\Images\loader.gif”,但错误是“找不到文件 'C:\Program Files (x86)\ IIS Express\loader.gif'。”如何解决它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
相关资源
最近更新 更多