目前想写个程序将上传的图片文件转换为二进制形式的字符串,让后将他存储在一个隐藏字段中,用的时候去提取,用完后再将它保存到数据库中去,那位大哥大姐们有这方面的程序啊,贴出来参考参考
二进制转换成图片是不要单独写个文件,然后让图片控件的url连接这个文件地址啊?
if (size.Equals("PicSmall"))
strsql = "update product set PicSmall=@Pic where barcode = '" + barcode + "'";
else
strsql = "update product set PicBig=@Pic where barcode = '" + barcode + "'";
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["Release"].ConnectionString;
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(strsql, conn);
byte[] imagebyte = new byte[file.PostedFile.InputStream.Length];
file.PostedFile.InputStream.Read(imagebyte, 0, imagebyte.Length);
cmd.Parameters.Add("@Pic", SqlDbType.Image).Value = imagebyte;
conn.Open();
cmd.ExecuteNonQuery();
从数据库中取出,然后在页面显示,怎么在转化回来了呢?是不是要一个单独的文件啊?将用src连接这个单独的文件?
@错误的过客: public partial class Picture : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strPicField = Request.QueryString["type"] != "1" ? "PicSmall" : "PicBig";
byte[] myData = new ProductDal().GetProductPic(Request["image"],strPicField);
Response.ContentType = "image/jpg";
if (myData.Length > 0)
Response.OutputStream.Write(myData, 0, myData.Length);
//Response.BinaryWrite(myData);
Response.End();
}
}
定義一個Picture.aspx文件。或則ashx文件
<asp:ImageButton runat="server" ImageUrl='<%# "~/Modules/Web/Picture.aspx?image="+ Eval("barcode")%>'
應該很清楚了吧。
public ActionResult SaveStaffImage(string flag)
{
string StaffID = Request["staffID"].ToString();
string func = Request["func"].ToString();
ISpecification<Staff> condition = new DirectSpecification<Staff>(x => x.ID == StaffID);
Staff model = _staffSrv.GetByCondition(condition);
if (model != null)
{
foreach (string upload in Request.Files)
{
if (!(Request.Files[upload].ContentLength > 0))
continue;
Stream fileStream = Request.Files[upload].InputStream;
int fileLength = Request.Files[upload].ContentLength;
byte[] fileData = new byte[fileLength];
fileStream.Read(fileData, 0, fileLength);
if (flag == "0")
{
model.ImageUP = fileData;
}
else
{
model.ImageDown = fileData;
}
int i = _staffSrv.Modify(model);
}
}
return View("EditStaffImage",model);
}
public FileContentResult GetStaffImage( )
{
string StaffID = Request["staffId"].ToString();
string flag = Request["flag"].ToString();
byte[] fileContent = null;
string mimeType = "";
string fileName = "";
ISpecification<Staff> condition = new DirectSpecification<Staff>(x => x.ID == StaffID);
Staff model = _staffSrv.GetByCondition(condition);
if (model != null)
{
if (flag == "0")
{
fileContent = model.ImageUP;
}
else
{
fileContent = model.ImageDown;
}
mimeType = "image/pjpeg";//没有在数据中添加这个字段 已定义死了
fileName = "tempImageName";
}
return File(fileContent, mimeType, fileName);
}
页面 图片<img src="/EMP/Staff/GetStaffImage?staffId=<%= model.ID %>&flag=0" alt="" />