将网上整理的代码发一下,试用过。但是提交文件时,路径问题没有处理好。
//增加操作
private bool DoAdd()
{
bool result = false;
Model.shbt_sh model = new Model.shbt_sh();
model.ryid =Convert.ToInt32(ryidtxt.Text.Trim());
model.btny =Convert.ToDateTime(btnytxt.Text.Trim());
model.shxm = shxmtxt.Text.Trim();
string fullPath = FileUploadImg.FileName;
model.tp = Base64Code(fullPath);
if (new DAL.shbt_sh().Add(model) > 0)
{
result = true;
}
return result;
}
//将图片Base64加密
public string Base64Code(String path)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)fs.Length);
return Convert.ToBase64String(bytes);
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!DoAdd())
{
Response.Write("<script>alert('错误!');</script>");
return;
}
else
{
Response.Write("<script>alert('保存成功!');var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index); parent.location.reload();</script>");
}
}