本文介绍了错误为Base64字符串转换为图像C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的网页上显示的图像,并同时单击链接按钮应该下载。图像文件存储的形式存储在数据库二进制格式。而BASE64字符串转换为图像它显示错误。细节在下面给出。帮我找一个妥善的解决办法。谢谢你。

I want to display image on my web page and while clicking link button that should download. The image file stored stored as binary format in db. while converting base64 string to image it showing an error. Details are given below. Help me to find a proper solution. Thank you.

错误:

code:

 protected void Page_Load(object sender, EventArgs e)
  {
   if (!IsPostBack)
     {

      pdfDataSetTableAdapters.tbl_imgTableAdapter td;
      td = new pdfDataSetTableAdapters.tbl_imgTableAdapter();
      DataTable dt = new DataTable();
      dt = td.GetId();
      DropDownList1.DataSource = dt;
      DropDownList1.DataTextField = "Id";
      DropDownList1.DataValueField = "Id";
      DropDownList1.DataBind();
      DropDownList1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select Id--", ""));
     }

  }
  protected void Button1_Click(object sender, EventArgs e)
   {
     pdfDataSetTableAdapters.tbl_imgTableAdapter td;
     td = new pdfDataSetTableAdapters.tbl_imgTableAdapter();
     DataTable dt = new DataTable();
     dt = td.GetImg(int.Parse(DropDownList1.SelectedValue));
     foreach (DataRow row in dt.Rows)
      {
         byte[] img2 = (byte[])row["img"];
         string base2 = Convert.ToBase64String(img2);
         Image1.ImageUrl = "data:image/jpg;base64," + base2;
       }
   }

protected void LinkButton1_Click(object sender, EventArgs e)
   {
        string sFile = Image1.ImageUrl;
        if (string.IsNullOrEmpty(sFile))
        {
            return;
        }
        FileInfo fi = new FileInfo(Server.MapPath(sFile)); // error popup here
        if (!fi.Exists)
        {
            return;
        }
        if (!string.IsNullOrEmpty(sFile))
        {
            // check if the file is an image
            NameValueCollection imageExtensions = new NameValueCollection();
            imageExtensions.Add(".jpg", "image/jpeg");
            imageExtensions.Add(".gif", "image/gif");
            imageExtensions.Add(".png", "image/png");
            if (imageExtensions.AllKeys.Contains(fi.Extension))
            {
                Response.ContentType = imageExtensions.Get(fi.Extension);
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + fi.Name);
                Response.TransmitFile(fi.FullName);
                Response.End();
            }
            Response.Redirect(sFile);
        }

.aspx的:

Aspx:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"> </asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Show" />
<asp:LinkButton ID="LinkButton1" runat="server">Download</asp:LinkButton>
<asp:Image ID="Image1" runat="server" />

的Sql QUERY1:

Sql query1:

SELECT Id FROM tbl_img

的Sql QUERY1:

Sql query1:

SELECT img FROM tbl_img WHERE (Id = @Id)

DB:

O / P屏幕:

推荐答案

当用户点击下载链接,你尝试打开Image1.URL,这是一个dataURL,而不是实际的路径。这将导致MapPath的失败。

When the user clicks the "download" link, you try to open the Image1.URL, which is a dataURL, not an actual path. This causes MapPath to fail.

之所以Image1.URL是dataURL是你设置当你点击按钮1(显示)图像到dataurl。

The Reason why Image1.URL is a dataURL is that you set the image to a dataurl when you click button 1 (show).

你可以做的是解析dataURL到一个文件并发送

What you can do is parse the dataURL to a file and send that

protected void LinkButton1_Click(object sender, EventArgs e){
    var data = Image1.ImageUrl.split('base64,')[1];
    File f = Parse.File("filename.jpg", {"base64": data});
    Response.Clear();
    Response.ContentType = "image/jpg";
    Response.WriteFile(f);
    Response.Flush();
    Response.End();
}

这篇关于错误为Base64字符串转换为图像C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:58