最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2009年12月22日 22:03:19   作者:  
asp.net 上傳下載輸出二進(jìn)制流實(shí)現(xiàn)代碼,需要的朋友可以參考下。
復(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ù)庫(kù)
/// </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ù)庫(kù)中 動(dòng)態(tài)實(shí)現(xiàn)類型
Response.OutputStream.Write(by, 0, by.Length); //輸出
Response.Flush();
}
conn.Close();
}
}
}

相關(guān)文章

最新評(píng)論

锡林郭勒盟| 锡林郭勒盟| 繁昌县| 留坝县| 西昌市| 靖宇县| 扬中市| 荔波县| 孙吴县| 广元市| 尉犁县| 云梦县| 平凉市| 简阳市| 南川市| 潢川县| 怀集县| 汤原县| 新民市| 金坛市| 阿荣旗| 乌鲁木齐市| 陕西省| 乌海市| 尚义县| 香港| 右玉县| 博湖县| 临高县| 丹东市| 南通市| 彭州市| 林芝县| 扎鲁特旗| 辽源市| 安新县| 许昌市| 通化县| 泌阳县| 永宁县| 怀柔区|