.net 中的 blob 下载计数器

发布于 2024-08-13 16:13:44 字数 1401 浏览 1 评论 0原文

我需要添加一个下载计数器来了解从数据库读取和显示我的 BLOB 数据的次数(以确定流量)。如何以及在哪里添加此计数器?非常感谢!

我有一个动态生成的链接列表,例如 文档文件名,指向显示页面。

我的显示页面代码如下所示:

Protected Sub 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

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2024-08-20 16:13:44

希望这会对您有所帮助。这是用于上传的,但也许您可以将其用作下载的参考。

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);
        }
    }

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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文