请从数据库二进制文件 [英] download binary file from database

查看:100
本文介绍了请从数据库二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库的二进制文件。
我怎样才能让一个下载这个文件的页面的标题单击按钮时

I have a binary file in database. How i can make a download of this file in header of page when a button is clicked

任何想法??

推荐答案

好吧,有几件事情你可能会想要做什么,它不是完全清楚,但我会在它采取刺伤。比方说,你已经有了一个包含文件标识符和二进制文件信息的以下基本数据库:

Alright, there's a couple of things you might be trying to do, its not completely clear but I'll take a stab at it. Let's say you've got the following basic database comprised of a file identifier and the binary file information:

CREATE TABLE Test
(
FileId int identity(1,1) PRIMARY KEY,
FileData varBinary(max)
)

您需要的是一个页面(在这种情况下,所谓的GetFile.aspx)用在了code-背后的Page_Load事件如下:

What you need is a page (in this case called "GetFile.aspx") with the following in the the code-behind's Page_Load event:

    'Make sure we've got a querystring
    If String.IsNullOrEmpty(Request.QueryString("fileid")) Then
        Throw New ApplicationException("Missing querystring parameter FileId")
    End If
    Dim FileId As Integer
    'Make sure the querystring is an Int
    If Not Integer.TryParse(Request.QueryString("fileid"), FileId) Then
        Throw New ApplicationException("Malformed querystring parameter FileId")
    End If
    'Change YourDsnHere to point to your database
    Using Con As New SqlConnection("YourDsnHere")
        Con.Open()
        'Select the file
        Using Com As New SqlCommand("SELECT * FROM Test WHERE FileId=@FileId", Con)
            Com.CommandType = Data.CommandType.Text
            Com.Parameters.AddWithValue("@FileId", FileId)
            Using RDR = Com.ExecuteReader()
                If RDR.Read Then
                    'Get the data out as a byte array
                    Dim Bytes() = DirectCast(RDR.Item("FileData"), Byte())
                    'Clear all response headers
                    Response.Clear()
                    'Set the type, my sample is a RAR file, you'll need to look up MIME types for yours
                    Response.AddHeader("Content-Type", "application/x-rar-compressed")
                    'Set the name of the file, if you don't set it browsers will use the name of this page which you don't want
                    Response.AddHeader("Content-Disposition", "inline; filename=YourFilenameHere.rar")
                    'Optional but nice, set the length so users get a progress bar
                    Response.AddHeader("Content-Length", Bytes.Count.ToString())
                    'Push the bytes out raw
                    Response.BinaryWrite(Bytes)
                    'Close the response stream so no more HTML gets accidentally pushed out
                    Response.End()
                Else
                    'Error, no file was found
                End If
            End Using
        End Using
        Con.Close()
    End Using

然后在你的页面只是当人点击这个按钮引导他们到这个页面并确保通过有效的 FILEID 的查询字符串。

就像我说的,我觉得这是你要找的东西,如果没有,说明你的描述上面

Like I said, I think this is what you're looking for, if not, clarify your description above

这篇关于请从数据库二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆