上传图片到数据库,然后在页面中显示出来,新手求教啊,好心人帮帮忙

解决方案 »

  1.   

    上传存储代码如下 protected void UpLoadFile(FileUpload fu, TextBox tb, string fileDir, Image img)
        {
            if (fu.HasFile)
            {
                string fileContentType = fu.PostedFile.ContentType;
                if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
                {
                    string name = fu.PostedFile.FileName; // 客户端文件路径
                    FileInfo file = new FileInfo(name);
                    //取得当前日期时间
                    string strDate = DirFile.GetDateDir();
                    string strTime = DirFile.GetDateFile();
                    //创建目录
                    DirFile.CreateDir(fileDir + strDate);
                    string fileName = strDate + strTime + file.Name.Substring(file.Name.LastIndexOf(".")); // 文件名称file.Name
                    //string fileName_s = strDate + strTime + "s" + file.Name.Substring(file.Name.LastIndexOf(".")); // 文件名称file.Name
                    string webFilePath = Server.MapPath("~" + fileDir + strDate + "/" + fileName); // 服务器端文件路径
                    //string webFilePath_s = Server.MapPath("/UploadFiles/upload/news/" + strDate + "/" + fileName_s);// 服务器端缩略图文件路径
                    if (!File.Exists(webFilePath))
                    {
                        try
                        {
                            fu.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
                            //pic.MakeThumbnail(webFilePath, webFilePath_s, 250, 220, "HW"); // 生成缩略图方法
                            tb.Text = strDate + "/" + fileName;
                            PublicFc.Show(this, "恭喜你,文件上传成功!");
                            //DirFile.DeleteFile("/UploadFiles/upload/news/" + strDate + "/" + fileName);
                            img.ImageUrl = "../.." + fileDir + this.txtImg.Text;
                        }
                        catch (Exception ex)
                        {
                            PublicFc.Show(this, "提示:文件上传失败,失败原因:" + ex.Message);
                        }
                    }
                    else
                    {
                        PublicFc.Show(this, "提示:文件已经存在,请重命名后上传!");
                    }
                }
                else
                {
                    PublicFc.Show(this, "提示:文件类型不符,只支持.gif、.bmp和.jpg图片类型!");
                }
            }
        }
      

  2.   

    前台代码、人去吃饭了! <tr>
                        <td style="height: 25px; width: 100px; text-align: right;">
                            新闻图片:</td>
                        <td>
                            <asp:Image ID="imglink_pic" runat="server" Width="150px" Height="120px" />
                            <br />
                            <asp:TextBox ID="txtImg" runat="server" Width="250px" ReadOnly="True"></asp:TextBox>
                            <br />
                            <asp:FileUpload ID="fuImg" runat="server" Width="260px" />
                            <asp:Button ID="btnImgUpload" runat="server" Text="上传" OnClick="btnImgUpload_Click" />
                            *上传前请注意比例,防止图片过大或变形*
                        </td>
                    </tr>
      

  3.   

    简单一点说吧:
    本地图片访问的路径是:http://localhost/images/20120107/test.jpg数据库存的路径是:http://localhost/images/20120107/test.jpg   这是存的绝对的路径,在前台直接绑定出来就行了。数据库存的路径是:20120107/test.jpg   这是存的相对的路径,要用相应路径来绑定。
    前台绑定代码:<image src="/<%=imagpath%>">如果有不明白的,请加QQ:79522860
      

  4.   

    如果你是把图片文件直接以二进制存到数据库,要显示图片就是读二进制流写图片,可参考:
    http://www.2cto.com/kf/201109/103586.html
    http://blog.163.com/sdq_1314/blog/static/166908254201081704330711/如果是存的路径请参考hailang2ll的思路
      

  5.   

    <asp:DataList
            id="dlstImages"
            DataSourceID="srcImages"
            RepeatColumns="3"
            Runat="server">
            <ItemTemplate>
            <asp:Image ID="Image1"
                ImageUrl='<%# String.Format("DynamicImage.ashx?id={0}", Eval("Id")) %>'
                Width="250"
                Runat="server" />
            <br />
            <%# Eval("Description") %>
        </ItemTemplate>
        </asp:DataList>
        <hr />    <asp:FormView
            id="frmImage"
            DataSourceID="srcImages"
            DefaultMode="Insert"
            Runat="server">
            <InsertItemTemplate>
            <asp:Label
                id="lblImage"
                Text="Upload Image:"
                AssociatedControlId="upImage"
                Runat="server" />
            <br />    
            <asp:FileUpload
                id="upImage"
                FileBytes='<%# Bind("Image") %>'
                Runat="server" />
            
            <br /><br />
            
            <asp:Label
                id="lblDescription"
                Text="Description:"
                AssociatedControlID="txtDescription"
                Runat="server" />
            <br />    
            <asp:TextBox
                id="txtDescription"
                Text='<%# Bind("Description") %>'
                TextMode="MultiLine"
                Columns="50"
                Rows="2"
                Runat="server" />    
            
            <br /><br />
            
            <asp:Button
                id="btnInsert"
                Text="Add Image"
                CommandName="Insert"
                Runat="server" />    
            </InsertItemTemplate>
        </asp:FormView>    
            
       
        
        <asp:SqlDataSource
            id="srcImages"
            SelectCommand="SELECT ID,Description FROM Images"
            InsertCommand="INSERT Images (Image,Description)
                VALUES (@Image,@Description)"
            ConnectionString="<%$ ConnectionStrings:Images %>"
            Runat="server" />
    ------------------------------------------------------------------
    ashx
    <%@ WebHandler Language="C#" Class="DynamicImage" %>using System.Data;
    using System.Web;
    using System.Web.Configuration;
    using System.Web.UI;
    using System.Web.UI.WebControls;/// <summary>
    /// Displays an image corresponding to the Id passed
    /// in a query string field
    /// </summary>
    public class DynamicImage : IHttpHandler 
    {
        
        public void ProcessRequest (HttpContext context) 
        {
            // Get the Id of the image to display
            string imageId = context.Request.QueryString["Id"];
            
            // Use SqlDataSource to grab image bytes
            SqlDataSource src = new SqlDataSource();
            src.ConnectionString = WebConfigurationManager.ConnectionStrings["Images"].ConnectionString;
            src.SelectCommand = "SELECT Image FROM Images WHERE Id=" + imageId;        // Return a DataView
            
            DataView view = (DataView)src.Select(DataSourceSelectArguments.Empty);
            context.Response.BinaryWrite( (byte[])view[0]["Image"]);        // Return a DataReader
            //src.DataSourceMode = SqlDataSourceMode.DataReader;
            //IDataReader reader = (IDataReader)src.Select(DataSourceSelectArguments.Empty);
            //reader.Read();
            //context.Response.BinaryWrite((byte[])reader["Image"]);
            //reader.Close();
            
        }
     
        public bool IsReusable 
        {
            get 
            {
                return false;
            }
        }}
      

  6.   

    1、上传图片时,保存程序中图片的相对路径2、上传时直接 转化 保存数据流  byte,存储到数据库中,
    显示时在请求转化,