我在数据库类型的数据库中有一个字段,它以二进制格式存储文件。
现在我想向用户提供一个设施,用户可以从该文件下载该文件。 我有字节数组对象中的文件数据。 如何向用户显示打开的保存拨号框?
答案 0 :(得分:0)
这是一个显示来自数据库的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();
}
}
答案 1 :(得分:0)
尝试看看:创建ashx文件处理程序,使用标题提供图像:
Content-Type: application/force-download
答案 2 :(得分:0)
怎么样:
//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);
修改强>
//using System.IO;
MemoryStream ms = new MemoryStream(array);
Bitmap bmp = new Bitmap(ms);
bmp.Save("C:\\image.bmp");