【问题标题】:display image while upload in asp.net在asp.net中上传时显示图像
【发布时间】:2014-05-21 14:49:43
【问题描述】:

我在注册页面的 asp.net 中创建了一个应用程序,我想在该页面上上传图片并同时保存在数据库中,但是数据库工作已经完成,我可以保存我上传的图片,但无法在该页面上显示。

我的设计代码:

 <form id=`form 1`  run at="server">
    <div>
    <table>
    <tr>
    <td>
         <asp:FileUpload ID="FileUpload1" run at="server" />
    </td>
    <td>
        <asp:Button ID="Button1" runat="server" Text="submit" onclick="Button1_Click" /></td>
        <td>
            <asp:Label ID="Label1" runat="server" Text="" Font-Names = "Arial"></asp:Label>
            <asp:Image ID="Image1" runat="server" Height="52px" Width="79px" />
        </td></tr></table>

    </div>
    </form>

我的代码:

    protected void Button 1_Click(object sender, EventArgs e)
    {
        // Read the file and convert it to Byte Array
        string filePath = FileUpload1.PostedFile.FileName;
        string filename = Path.GetFileName(filePath);
        string ext = Path.GetExtension(filename);
        string contenttype = String.Empty;
        Image1.ImageUrl = filename;
        //Set the contenttype based on File Extension
        switch (ext)
        {
            case ".doc":
                contenttype = "application/vnd.ms-word";
                break;
            case ".docx":
                contenttype = "application/vnd.ms-word";
                break;
            case ".xls":
                contenttype = "application/vnd.ms-excel";
                break;
            case ".xlsx":
                contenttype = "application/vnd.ms-excel";
                break;
            case ".jpg":
                contenttype = "image/jpg";
                break;
            case ".png":
                contenttype = "image/png";
                break;
            case ".gif":
                contenttype = "image/gif";
                break;
            case ".pdf":
                contenttype = "application/pdf";
                break;
        }
        if (contenttype != String.Empty)
        {

            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            //insert the file into database
            string strQuery = "insert into empimage(Name, ContentType, Data)" +
               " values (@Name, @ContentType, @Data)";
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
            cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
              = contenttype;
            cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
            InsertUpdateData(cmd);
            Label1.ForeColor = System.Drawing.Color.Green;
            Label1.Text = "File Uploaded Successfully";
        }
        else
        {
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Text = "File format not recognised." +
              " Upload Image/Word/PDF/Excel formats";
        }

}
    private Boolean InsertUpdateData(SqlCommand cmd)
    {
        pd = ConfigurationManager.ConnectionStrings["su"].ConnectionString;
        con = new SqlConnection(pd);
        //String strConnString = System.Configuration.ConfigurationManager
        //.ConnectionStrings["conString"].ConnectionString;
        //SqlConnection con = new SqlConnection(strConnString);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            return false;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }

但它在运行时不显示图像

请给我建议

【问题讨论】:

标签: c# asp.net file-upload


【解决方案1】:

你必须尝试像

Image1.ImageUrl = FileUpload1.FileName;

【讨论】:

  • 您能否简要解释一下您的解决方案是如何工作的。这将使您的回答对 OP 和其他读者更有帮助(+1)。
  • 我尝试了这段代码,但它不起作用,它只显示图像图标而不是图像,但我找到了我的问题的答案,代码受保护 void Button1_Click(object sender, EventArgs e) { if (FileUpload1. PostedFile != null && FileUpload1.PostedFile.FileName != "") { FileUpload1.SaveAs(Server.MapPath("image/" +FileUpload1.FileName)); Image1.ImageUrl = "~/image/" + FileUpload1.FileName; } cmd =new SqlCommand("插入到empimage(Name) values('"+"~/Image/"+ FileUpload1.FileName+"')",con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); }
猜你喜欢
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多