简体   繁体   English

从文件流下载文件

[英]Download file from filestream

I have a field in database of type image which stores the file in binary format. 我在image类型的数据库中有一个字段,该字段以二进制格式存储文件。

Now I want to give a facility to user from where the user can downlaod that file. 现在,我想为用户提供一种便利,用户可以从中下载该文件。 I have the file data in byte array object. 我在字节数组对象中有文件数据。 How can I display the open save dialoge box to user? 如何向用户显示打开保存对话框?

This is an example to show blob images from the database, you can modify it to offer the file for download instead of showing the image (id is the image file id in the database): 这是显示数据库中Blob图像的示例,您可以对其进行修改以提供文件下载而不是显示图像(id是数据库中的图像文件ID):

void  CreateImage(string id)
{
    // Connection string is taken from web.config file.
    SqlConnection _con = new SqlConnection(
      System.Configuration.ConfigurationSettings.AppSettings["DB"]);

    try
    {
        _con.Open();
        SqlCommand _cmd = _con.CreateCommand();
        _cmd.CommandText = "select logo from" + 
                           " pub_info where pub_id='" + 
                           id + "'";
        byte[] _buf = (byte[])_cmd.ExecuteScalar();

        // This is optional
        Response.ContentType = "image/gif";

        //stream it back in the HTTP response
        Response.BinaryWrite(_buf);


    }
    catch
    {}
    finally
    {
        _con.Close();

    }
}

尝试看一下:创建ashx文件处理程序,提供带有标头的图像:

Content-Type: application/force-download

What about: 关于什么:

//using System.Net;   
WebClient wc = new WebClient();

OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog() == DialogResult.OK)
   wc.DownloadFile("http://website.net/image.jpg", ofd.FileName);

EDIT: 编辑:

//using System.IO;
MemoryStream ms = new MemoryStream(array);
Bitmap bmp = new Bitmap(ms);
bmp.Save("C:\\image.bmp");

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

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