ファイルをバイナリ形式で保存するタイプのイメージのデータベースにフィールドがあります。
ここで、ユーザーがそのファイルをダウンロードできる場所をユーザーに提供したいと考えています。バイト配列オブジェクトにファイルデータがあります。開いている保存ダイアログボックスをユーザーに表示するにはどうすればよいですか?
ファイルをバイナリ形式で保存するタイプのイメージのデータベースにフィールドがあります。
ここで、ユーザーがそのファイルをダウンロードできる場所をユーザーに提供したいと考えています。バイト配列オブジェクトにファイルデータがあります。開いている保存ダイアログボックスをユーザーに表示するにはどうすればよいですか?
これは、データベースから 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
どうですか:
//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");