简体   繁体   中英

display image while upload in asp.net

I make an application in asp.net in registration page i want to upload the picture on that page and save in database simultaneously but the database work has done im able to save my upload image but can't show on that page.

My designing code :

 <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>

my code behind :

    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();
        }
    }

but it does not show the image at run time

please suggest me

你必须尝试像

Image1.ImageUrl = FileUpload1.FileName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM