当前位置: 代码迷 >> ASP.NET >> ASP.NET+SQLServer上传并显示照片,该如何解决
  详细解决方案

ASP.NET+SQLServer上传并显示照片,该如何解决

热度:1345   发布时间:2013-02-25 00:00:00.0
ASP.NET+SQLServer上传并显示照片
开发工具:VS2005
数据库:SQLServer2000
开发语言:C#
问题:在一个ASP.Net页中实现上传照片并存入数据库,上传后显示所上传的照片,以后每次登录到相关页面时都会读取出此照片并显示出来
请大家说得详细些,易懂些

------解决方案--------------------------------------------------------
干吗不存在本地硬盘就好,再用路径把它取出来,
存到数据库的,占用数据库容量挺大的
------解决方案--------------------------------------------------------
直接在服务端建个文件夹,这样直接上传到该文件夹,就像一楼说的那样,用路径把它读取出来!
------解决方案--------------------------------------------------------
建议你看我发过的帖子,我发过的帖子有跟你一样的问题,在我的帖子里有贴我的代码。
代码只是一个思路!
------解决方案--------------------------------------------------------
C# code
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {            }    protected void Button1_Click(object sender, EventArgs e)    {        #region 测试写入        if (!FileUpload1.HasFile)        {            Response.Write("请选择图片");        }            string fileEx = System.IO.Path.GetExtension(this.FileUpload1.FileName);  //获取图片扩展名            if (fileEx != ".gif" && fileEx != ".jpg" && fileEx != ".jpeg" && fileEx!= ".bmp")            {                Response.Write("只能上传图片");            }            else            {                                    // ImageType 有用,读取图片的时候需要                     int imgSize = this.FileUpload1.PostedFile.ContentLength;  //图片大小 (字节)                    string imgType = this.FileUpload1.PostedFile.ContentType;    //图片类型                    string imgPath = this.FileUpload1.PostedFile.FileName; //获取图片路径                    string imgName = this.FileUpload1.FileName; //获取图片名字                    int imgLength = this.FileUpload1.FileName.Length; //获取图片长度                    if (imgLength <= 0)//判断是否有图片                        return;                    Byte[] imgByte = new Byte[imgSize]; //用图片的长度来初始化一个字节数组存储临时的图片文件                    Stream stream = this.FileUpload1.PostedFile.InputStream; //建立文件流对象                    stream.Read(imgByte, 0, imgSize);// 读取图片数据到临时存储体imgByte,0为数据指针位置,fileLength为数据长度                    //this.FileUpload1.FileBytes                    try                    {                        string connection = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;                        SqlConnection sqlconn = new SqlConnection(connection);                        sqlconn.Open();                        string sqlcmd = "insert into Image values (@ImageType,@ImageData,@ImageTitle)";                        SqlCommand sqc =
------解决方案--------------------------------------------------------
C# code
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;using System.Data.SqlClient;public partial class Default2 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!Page.IsPostBack)        {            #region  测试读取            string sqlCmd = "select top 1 * from Image order by imageID desc";            string connection = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;            SqlConnection sqlconn = new SqlConnection(connection);            try            {                sqlconn.Open();                SqlCommand sqc = new SqlCommand(sqlCmd, sqlconn);                //Response.ContentType = "image/pjpeg";                using (SqlDataReader sdr = sqc.ExecuteReader())                {                    while (sdr.Read())                    {                        //Response.ContentType = sdr["imageType"].ToString();                        Response.Clear();                        Response.BinaryWrite((byte[])sdr["imageData"]);                    }                }                //sdr.Close();                //sqlconn.Close();            }            catch (Exception ex)            {                Response.Write(ex.StackTrace);            }            finally            {                sqlconn.Close();            }            #endregion        }    }}
  相关解决方案