简体   繁体   English

使用c#asp.net从sql下载blob文件,并向用户显示保存对话框

[英]download blob file from sql using c# asp.net and present a save dialog to user

I've been searching the web now for a couple of hours. 我已经在网上搜索了两个小时。 Although it seems very easy to upload a blob file to a sql database, it's a nightmare trying to download it again. 尽管将blob文件上传到sql数据库似乎很容易,但是尝试再次下载它是一场噩梦。

I have a gridview displaying records. 我有一个显示记录的gridview。 The grid view has a linkbutton which I want to use to download a blob file wich is saved in the same table as where the gridview is loading its data from. 网格视图具有一个链接按钮,我想使用该按钮下载blob文件,但该文件保存在与gridview从中加载其数据的位置相同的表中。 I'm passing the records id to my code-behind function with the onclick event. 我通过onclick事件将记录id传递给我的代码隐藏函数。

here is my code behind for the on click event 这是我的on click事件的代码

protected void Downloadbutton_Click(Object sender, CommandEventArgs e)
    {

        string reqid = e.CommandArgument.ToString();

        using (SqlConnection connection = new SqlConnection("ConnectionString"))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select Attached_File_Name from ABSENCE_REQUEST where Request_ID = @Request_ID";
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("@Request_ID", Convert.ToInt32(20057));

            byte[] buffer = (byte[]) cmd.ExecuteScalar();
            using (FileStream fs = new FileStream(@"C:\test.pdf", FileMode.Create))
            {
                fs.Write(buffer, 0, buffer.Length);
            }
        }
    }

I know that in my code I actually set a download location for the file. 我知道在我的代码中实际上设置了文件的下载位置。 But how can I change it so the user will be asked where to save the file? 但是,如何更改它,以便系统询问用户将文件保存在哪里?

I think you can simply write the data to the Response (not tested), something like: 我认为您可以简单地将数据写入Response(未经测试),例如:

private void WriteFile (Byte[] bytes, string fileName)
{
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = dt.Rows[0]["ContentType"].ToString();
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}

You may also want to consider creating a generic handler for this, so your code would be pretty similar, but you can then have links to files rather than only available on button clicks, eg your link might be: 您可能还想考虑为此创建一个通用处理程序,因此您的代码将非常相似,但是您可以具有指向文件的链接,而不仅仅是单击按钮时可用,例如,您的链接可能是:

YourWebsite/Downloads/RequestFileHandler.ashx?RequestID=5

To download the file where the request_ID is 5 下载request_ID为5的文件

EDIT 编辑

Sorry, completely assumed this was for a web page, however have now realised it may not be, in which case you could use something like: 抱歉,完全假设这是用于网页,但是现在已经意识到可能不是,在这种情况下,您可以使用类似以下内容的方法:

private bool SaveBytesToFile(byte[] butes)
{
    var saveFileDialog = new SaveFileDialog();
    if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
    {
        using (var fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write))
        {
            fileStream.Write(bytes, 0, bytes.Length);
        }
        return true;
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM