• asp.net实现sql存取图片


    有一个员工表Employee,需要保存员工照片(Photo)到数据库(sql server)上。员工照片对应的字段是varbinary(max),也就是要存成二进制文件类型(这和以前讨巧地存图片文件路径就不相同了),默认可以为空。下面说说主要实现思路:
    1、存取图片
    (1)、将图片文件转换为二进制并直接存进sql server

         //UploadHelper.cs
    /// <summary>
    /// 将图片转化为长二进制
    /// </summary>
    /// <param name="photopath"></param>
    /// <returns></returns>
    public static Byte[] SetImgToByte(string imgPath)
    {
    FileStream file = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
    Byte[] byteData = new Byte[file.Length];
    file.Read(byteData, 0, byteData.Length);
    file.Close();
    return byteData;
    }

    /// <summary>
    /// 将转换成二进制码的图片保存到数据库中
    /// </summary>
    public static bool SaveEmployeeImg2Db(Employee model, string path)
    {
    try
    {
    Byte[] imgBytes = SetImgToByte(path);
    model.Photo = imgBytes;
    bool flag=EmployeeService.SaveEmployeePhoto(model); //EmployeeService是公司内部的库调用,插入或者更新照片,这里不透露细节
    return flag;

    }
    catch (Exception ex)
    {
    throw ex;
    }
    }
    2)、在网页中上传图片


    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload_Click(object sender, EventArgs e)
    {
    string serverPath = Server.MapPath("~/images/");
    if (this.fuPhoto.HasFile) //fuPhoto是fileupload控件
    {
    string fileName = this.fuPhoto.PostedFile.FileName;
    FileInfo fi = new FileInfo(fileName);
    string mimeType = this.fuPhoto.PostedFile.ContentType.ToLower();
    if (mimeType.IndexOf("image") < 0)
    {
    //("上传的照片格式不对");
    }
    else if(fi.Length > 2* 1024 * 1024)
    {
    //图片大于2M,重新处理
    }
    else
    {
    string saveFilePath = serverPath + DateTime.Now.ToString("yyyyMMddHHmmss") + fileName;
    try
    {
    //先存图片到服务器
    this.fuPhoto.PostedFile.SaveAs(saveFilePath);

    //转成二进制
    Employee model = new Employee(int.Parse(id)); //id是EmployeeId,这里是模拟字段
    bool flag = UploadHelper.SaveEmployeeImg2Db(model, saveFilePath);
    }
    catch
    {
    //("照片上传失败");
    }
    finally
    {
    //最后删掉该图片
    if (System.IO.File.Exists(saveFilePath))
    {
    System.IO.File.Delete(saveFilePath);
    }
    }
    }
    }
    else
    {
    //("全选择要上传的照片");
    }
    }
    3)、从数据库取出照片(返回格式Image)

    //////////////////Code
    上面的这个方法取出来之后,如果在winform下,直接给一个PictureBox的Image属性赋值就可以了。可是web下没有这么强大的控件,所以,就有了下面的步骤。
    2、直接在网页中以流的形式显示图片
    1)、生成图片流页面(ImgHelper .aspx)
    这个页面的设计页面什么也没有,类文件如下:


    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.Collections.Generic;
    using System.IO;

    /// <summary>
    /// 图片辅助类
    /// </summary>
    public partial class ImgHelper : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!string.IsNullOrEmpty(Request["employeeId"])) //需要显示照片的页面传递的员工id
    {
    int employeeId = int.Parse(Request["employeeId"]);
    Employee model = //EmployeeService.GetEmployeeByCondition(new Employee(employeeId))[0] as Employee; //内部函数 查找一个员工 不透漏细节
    try
    {
    Byte[] byteImg = model.Photo;
    Stream stream = new MemoryStream(byteImg);
    System.Drawing.Bitmap img =(System.Drawing.Bitmap) System.Drawing.Bitmap.FromStream(stream, false); //转换成Bitmap
    Response.Buffer = false;
    Response.ContentType = "image/jpg";
    Response.AddHeader("Content-Disposition", "attachment;filename=photo.jpg");//照片名称叫photo.jpg
    Response.BinaryWrite(byteImg);//写入二进制流
    Response.End();
    }
    catch
    {
    Response.End();
    }
    }
    }
    }
    2)、显示照片的页面调用ImgHelper .aspx
    在页面加载的时候,给图片控件赋值如下:

    this.imgPhoto.ImageUrl = "/ImgHelper.aspx?employeeId="+tmpEmployee.Id.ToString(); //imgPhoto是图片控件




    //成功一定有方法,失败一定有原因。
  • 相关阅读:
    RAID卡 BBU Learn Cycle周期的影响
    Linux下查看Raid磁盘阵列信息的方法
    ROS导航包的介绍
    ROS源码解读(二)--全局路径规划
    ROS源码解读(一)--局部路径规划
    VS运行release版本正常,直接执行exe文件会出现问题
    IFM设备 Linux方面资料
    Map-making Robots: A Review of the Occupancy Grid Map Algorithm
    Eigen 介绍及简单使用
    绘制二维障碍栅格地图
  • 原文地址:https://www.cnblogs.com/webapi/p/2415176.html
Copyright © 2020-2023  润新知