【问题标题】:asp.net file upload and file reading (Images)asp.net 文件上传和文件读取(图片)
【发布时间】:2013-12-26 21:31:28
【问题描述】:

我正在使用 asp.net 创建一个移动网络应用程序来管理自行车零件 我已经让文件上传工作了,但现在我需要弄清楚如何让图像控件从服务器上的新位置显示图像。

我会将图像文件路径保存在数据库中,我只需要弄清楚如何获取图像的新文件路径。

这是我用于文件上传的代码

if (this.FileUpload1.HasFile)
    {
      this.FileUpload1.SaveAs(Server.MapPath("~/Mobile/" + FileUpload1.FileName));
    }

我可能会解决这个问题,但以防万一我想不通我会在现在而不是稍后发布问题,因为可能需要一段时间才能得到答案,而且我有一个截止日期

【问题讨论】:

    标签: c# mysql asp.net file-upload filepath


    【解决方案1】:

    您将不得不使用“ImageHandler”来正确读取图像。

    这就是我的处理程序。

    public class ImageHandler : IHttpHandler
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dboBlog"].ConnectionString);
            public void ProcessRequest(HttpContext context)
            {
                try
                {
                    string messageid = context.Request.QueryString["mid"];
    
                conn.Open();
                SqlCommand command = new SqlCommand("SELECT Image from BlogMessages WHERE Image IS NOT NULL AND MessageID=" + messageid, conn);
                SqlDataReader dr = command.ExecuteReader();
    
                if (dr.Read())
                {
                    context.Response.BinaryWrite((Byte[])dr[0]);
                    conn.Close();
                    context.Response.End();
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    

    如您所知,我使用 QueryString。我使用这个查询字符串来调用图像。我在 gridview 中调用我的图像,但这就是它的外观......

    <asp:Image ID="postImage" runat="server" ImageUrl='<%# "ImageHandler.ashx?mid="+ Eval("MessageID") %>' Width="400px" AlternateText="No Image" ImageAlign="Middle" Visible="false" />
    

    我确实将可见性设置为 false,因为它是一个博客,有时人们不会上传图片。如您所知,图像 url 调用 ImageHandler,其中查询字符串等于 MessageID。

    这对我有用,所以希望它能帮助你。

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 2012-08-23
      • 2011-07-21
      • 2011-07-08
      • 2018-09-14
      • 2015-10-16
      相关资源
      最近更新 更多