我需要添加一个下载计数器,以了解从数据库中读取和显示我的BLOB数据的次数(以确定流量)。如何以及在哪里可以添加此计数器?非常感谢!
我有一个动态生成的链接列表,例如
<a href="page.aspx?DocID=IDhere">
文档文件名</a>
,指向显示页面。
我的显示页面代码如下:
受保护的子Page_Load
Dim DocID As Integer = Convert.ToInt32(Request.QueryString("DocID"))
Dim connStr As String = conn string 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
答案 0 :(得分:1)
希望这会帮助你。它是用于上传的,但也许您可以将其用作下载的参考。
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);
}
}