1新建页面
例如:Default1.aspx 在页面上添加图片标签
<asp:Image ID="Image1" runat="server" />
添加一个查看按钮
//查看按钮
protected void LinkButton1_Click(object sender, EventArgs e)
{
int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
int id = int.Parse(((Label)this.GridView1.Rows[row].FindControl("Label3")).Text);
int JobDetailID = int.Parse(((Label)this.GridView1.Rows[row].FindControl("Label4")).Text);
Checkkind = Request["Checkkind"].ToString();
pic.Style.Add("display", "block");
//传值给Default页面
Image1.ImageUrl = "Default.aspx?id=" + id + "&checkkind=" + Checkkind + "&JobDetailID=" + JobDetailID;
}
Default.aspx页面接收
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
string checkkind = Request.QueryString["checkkind"];
string JobDetailID = Request.QueryString["JobDetailID"];
SqlParameter[] para = {
new SqlParameter("@Idkey",id),
new SqlParameter("@CheckKind",checkkind),
new SqlParameter("@JobDetailID",JobDetailID)
};
string sProcName = "P_shiguImage_getData_ByIdkeyandCheckKind";
DataSet ds = new DataSet();
ds = SQLHelper.SqlHelper.ExecuteDataset(new GFunc().GetConnStr(), CommandType.StoredProcedure, sProcName, para);
if (ds.Tables[0].Rows.Count > 0)
{
Response.BinaryWrite((byte[])(ds.Tables[0].Rows[0]["piccontent"]));
Response.End();
}
}
下载按钮 :Default1.aspx
//下载按钮
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);
int JobDetailID = int.Parse(((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;
//以字符流的形式下载文件
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();
}
}