blob download counter in .net
I need to add a download counter to know how many times my BLOB data is read and displayed from the database (to determine traffic). How and where can I add this counter? Many thanks!
I have a dynamically generated list of links such as
<a href="page.aspx?DocID=IDhere">
Document filename</a>
which direct to a display page.
My display page code looks like:
Protected Sub Page_Load
Dim DocID As Integer = Convert.ToInt32(Request.QueryString("DocID"))
Dim connStr As String = conn string 开发者_StackOverflow社区here
Dim SqlCmd1 As String = "SELECT DocID, DocBD, Filename, MIMEType WHERE DocID=@DocID"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim Cmd1 As SqlCommand = New SqlCommand(sqlCmd1, conn)
With Cmd1.Parameters
.Add(New SqlParameter("@DocID", DocID)
End With
Try
conn.Open()
Dim myReader As SqlDataReader = Cmd1.ExecuteReader
If myReader.Read Then
Response.ClearContent()
Response.AddHeader("content-disposition", "inline; filename=" & myReader("Filename"))
Response.ContentType = myReader("MIMEType").ToString()
Response.BinaryWrite(myReader("DocBD"))
Response.End()
Else
Label1.Text = "The document you requested doesn't exist in the database. Please contact the document owner"
End If
myReader.Close()
Catch ex As Exception
Label1.Text = ex.Message()
Finally
conn.Close()
End Try
End Sub
Hopefully this will help you out. It's for uploading, but maybe you can use it as a reference for downloading.
public void ProcessRequest(HttpContext context, string uploadPath)
{
string filename = context.Request.QueryString["filename"];
bool complete = string.IsNullOrEmpty(context.Request.QueryString["Complete"]) ? true : bool.Parse(context.Request.QueryString["Complete"]);
bool getBytes = string.IsNullOrEmpty(context.Request.QueryString["GetBytes"]) ? false : bool.Parse(context.Request.QueryString["GetBytes"]);
long startByte = string.IsNullOrEmpty(context.Request.QueryString["StartByte"]) ? 0 : long.Parse(context.Request.QueryString["StartByte"]); ;
string filePath;
if (UniqueUserUpload)
{
if (context.User.Identity.IsAuthenticated)
{
filePath = Path.Combine(uploadPath, string.Format("{0}_{1}", context.User.Identity.Name.Replace("\\",""), filename));
}
else
{
if (context.Session["fileUploadUser"] == null)
context.Session["fileUploadUser"] = Guid.NewGuid();
filePath = Path.Combine(uploadPath, string.Format("{0}_{1}", context.Session["fileUploadUser"], filename));
}
}
else
filePath = Path.Combine(uploadPath, filename);
if (getBytes)
{
FileInfo fi = new FileInfo(filePath);
if (!fi.Exists)
context.Response.Write("0");
else
context.Response.Write(fi.Length.ToString());
context.Response.Flush();
return;
}
else
{
if (startByte > 0 && File.Exists(filePath))
{
using (FileStream fs = File.Open(filePath, FileMode.Append))
{
SaveFile(context.Request.InputStream, fs);
fs.Close();
}
}
else
{
using (FileStream fs = File.Create(filePath))
{
SaveFile(context.Request.InputStream, fs);
fs.Close();
}
}
if (complete)
{
if (FileUploadCompleted != null)
{
FileUploadCompletedEventArgs args = new FileUploadCompletedEventArgs(filename, filePath);
FileUploadCompleted(this, args);
}
}
}
}
private void SaveFile(Stream stream, FileStream fs)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
}
精彩评论