国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 上傳下載輸出二進制流實現代碼

2024-07-10 13:18:23
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
namespace Delegate
{
public partial class ReadStream : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 將上傳的文件轉換成二進制流保存在數據庫
/// </summary>
/// <param></param>
/// <param></param>
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ToString());
string strSql="insert into test(timage)values(@images)";//保存二進制流的方式需參數的形式
SqlCommand cmd = new SqlCommand(strSql, conn);
conn.Open();
string filepath = Server.MapPath("~/");
filepath += FileUpload1.FileName;
FileUpload1.SaveAs(filepath);
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);//讀取文件
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
cmd.Parameters.Add("@images", SqlDbType.Binary, photo.Length);//設置參數
cmd.Parameters["@images"].Value = photo;//設置值
if (cmd.ExecuteNonQuery() == 1)
{
Response.Write("添加成功!");
}
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ToString());
conn.Open();
string strSql = "select top 1 timage from test";
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
byte[] by = (byte[])dr[0];
Response.AddHeader("Content-Disposition", "attachment; filename=ceshi.txt");//設置讀取的文件頭
Response.AddHeader("Content-Length", by.Length.ToString());
Response.ContentType = "text/plain";//設置輸出類型 這里可以保存在數據庫中 動態實現類型
Response.OutputStream.Write(by, 0, by.Length); //輸出
Response.Flush();
}
conn.Close();
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延边| 东乌珠穆沁旗| 克山县| 增城市| 甘德县| 德江县| 茌平县| 普兰县| 武功县| 贡觉县| 平武县| 开平市| 剑阁县| 稻城县| 斗六市| 常德市| 辽阳县| 略阳县| 莱州市| 环江| 洛宁县| 札达县| 芷江| 建平县| 麻江县| 柯坪县| 镇坪县| 永兴县| 门头沟区| 江华| 宝兴县| 吴桥县| 敦煌市| 镶黄旗| 凤庆县| 安图县| 麻城市| 乌鲁木齐县| 西乌珠穆沁旗| 会理县| 南城县|