我已经为用户提供了下载提供商上传的图像的选项。单击下载按钮时,将下载图像。 但我还想存储点击下载按钮的次数。 为此,我输入了这段代码,但它没有用。
我尝试了几种方法,比如声明一个int变量并递增它。没工作。然后我尝试了这个。还是行不通。我做错了什么?
protected void btn_download_Click(object sender, EventArgs e)
{
var res = (byte[])Session["Image"];
Response.Clear();
MemoryStream ms = new MemoryStream(res);
Response.ContentType = "image/jpeg";
Response.AddHeader("content-disposition", "attachment;filename=Image.jpg");
Response.Buffer = true;
ms.WriteTo(Response.OutputStream);
Response.End();
try
{
SqlCommand command = new SqlCommand("UPDATE [ImageWithTags] SET DownloadCount = DownloadCount + 1 WHERE ImageID='" + DropDownList1.SelectedItem.Text + "'", con);
con.Open();
command.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
答案 0 :(得分:-1)
您也可以为此创建一个会话变量,并执行以下计算:
protected void btn_download_Click(object sender, EventArgs e)
{
int counter=0;
if(Session["counter"]!=null)
{
Session["counter"] = ++Convert.ToInt32(Session["counter"]);
counter = Convert.ToInt32Session["counter"];
}
else
Session["counter"] = 0;
// do your calculation here with variable counter
}