//下载按钮
protected void LinkButton2_Click(object sender, EventArgs e)
{
int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
int id = int.Parse(((Label)this.GridView1.Rows[row].FindControl("Label3")).Text);
string JobDetailID = (((Label)this.GridView1.Rows[row].FindControl("Label4")).Text);
Checkkind = Request["Checkkind"].ToString();
DataSet ds = new DataSet();
SqlParameter[] para = {
new SqlParameter("@Idkey",id),
new SqlParameter("@CheckKind",Checkkind),
new SqlParameter("@JobDetailID",JobDetailID)
};
string sProcName = "P_shiguImage_getData_ByIdkeyandCheckKind";
ds = SQLHelper.SqlHelper.ExecuteDataset(new GFunc().GetConnStr(), CommandType.StoredProcedure, sProcName, para);
if (ds.Tables[0].Rows.Count > 0)
{
string FileName = GetValueByDS(ds, "picname");
byte[] piccontent = (byte[])(ds.Tables[0].Rows[0]["piccontent"]);
try
{
MemoryStream m = new MemoryStream(piccontent);//定义并实例化一个内存流,来存放上传的图片二进制流
if (!File.Exists(Server.MapPath(".") + "\\images1"))
{
Directory.CreateDirectory(Server.MapPath(".") + "\\images1");
}
FileStream f = new FileStream(Server.MapPath(".") + "\\images1\\" + FileName, FileMode.Create);//把内存里的文件写入文件流
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
//InsertErrorLog("图片上传成功");
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.Write("图片上传失败");
}
string downimage = Server.MapPath(".") + "\\images1\\" + FileName;
//Open(downimage);
//string fileName = "123.jpg";//客户端保存的文件名
//string filePath = Server.MapPath("images/1.jpg");//路径
//以字符流的形式下载文件
FileStream fs = new FileStream(downimage, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
public void UploadFile(byte[] fs, string FileName)
{
try
{
MemoryStream m = new MemoryStream(fs);//定义并实例化一个内存流,来存放上传的图片二进制流
if (!File.Exists(Server.MapPath(".") + "\\images1"))
{
Directory.CreateDirectory(Server.MapPath(".") + "\\images1");
}
FileStream f = new FileStream(Server.MapPath(".") + "\\images1\\" + FileName, FileMode.Create);//把内存里的文件写入文件流
m.WriteTo(f);
m.Close();
f = null;
m = null;
//InsertErrorLog("图片上传成功");
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.Write("图片上传失败");
}
}