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

首頁 > 編程 > .NET > 正文

asp.net 上傳下載輸出二進(jìn)制流實(shí)現(xiàn)代碼

2020-01-18 01:00:28
字體:
供稿:網(wǎng)友
復(fù)制代碼 代碼如下:

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>
/// 將上傳的文件轉(zhuǎn)換成二進(jìn)制流保存在數(shù)據(jù)庫
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></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)";//保存二進(jìn)制流的方式需參數(shù)的形式
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);//設(shè)置參數(shù)
cmd.Parameters["@images"].Value = photo;//設(shè)置值
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");//設(shè)置讀取的文件頭
Response.AddHeader("Content-Length", by.Length.ToString());
Response.ContentType = "text/plain";//設(shè)置輸出類型 這里可以保存在數(shù)據(jù)庫中 動(dòng)態(tài)實(shí)現(xiàn)類型
Response.OutputStream.Write(by, 0, by.Length); //輸出
Response.Flush();
}
conn.Close();
}
}
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 西乌珠穆沁旗| 禹城市| 江油市| 依兰县| 会理县| 梅州市| 富川| 阳新县| 报价| 东乌珠穆沁旗| 定西市| 岳西县| 惠安县| 赣州市| 黄山市| 昭通市| 长岛县| 鹤峰县| 米脂县| 吉水县| 广水市| 秦皇岛市| 岚皋县| 芜湖县| 延边| 岳西县| 通化县| 叙永县| 盘山县| 台北市| 罗江县| 贞丰县| 华宁县| 蒲江县| 鲁山县| 贡觉县| 乌兰察布市| 鲁甸县| 宝丰县| 安化县| 安平县|