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ù)庫
/// </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ù)庫中 動態(tài)實(shí)現(xiàn)類型
Response.OutputStream.Write(by, 0, by.Length); //輸出
Response.Flush();
}
conn.Close();
}
}
}
您可能感興趣的文章:
- ASP.NET Core文件上傳與下載實(shí)例(多種上傳方式)
- 擁有網(wǎng)頁版小U盤 ASP.NET實(shí)現(xiàn)文件上傳與下載功能
- asp.net+jquery.form實(shí)現(xiàn)圖片異步上傳的方法(附j(luò)query.form.js下載)
- Asp.net實(shí)現(xiàn)MVC處理文件的上傳下載功能實(shí)例教程
- asp.net 多文件上傳,兼容IE6/7/8,提供完整代碼下載
- asp.net 上傳或下載當(dāng)文件名包含有特殊字符"#"的處理
- asp.net Web Services上傳和下載文件(完整代碼)
- ASP.NET中文件上傳下載方法集合
- ASP.NET實(shí)現(xiàn)文件上傳功能
- ASP.NET Core實(shí)現(xiàn)文件上傳和下載
相關(guān)文章
JS實(shí)現(xiàn)完美include加載功能代碼
在寫這個(gè)之前在網(wǎng)上搜索了些資料,發(fā)現(xiàn)以前寫的include都存在2個(gè)問題,這也是include需要解決的比較重要的2個(gè)問題。2010-10-10MVC+EasyUI+三層新聞網(wǎng)站建立 分頁查詢數(shù)據(jù)功能(七)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第七篇,教大家如何分頁查詢出數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07高效的.Net UDP異步編程實(shí)現(xiàn)分析
重點(diǎn)是怎么建立一種高效的UDP機(jī)制來實(shí)時(shí)接收服務(wù)器發(fā)送過來的數(shù)據(jù)包.本文將介紹.Net UDP異步編程如何實(shí)現(xiàn)解決方案,有需求的朋友可以參考下2012-11-11ASP.NET AJAX 1.0 RC開發(fā)10分鐘圖解
12月15日,ASP.NET AJAX 1.0 RC版發(fā)布,我下載安裝試用了一下,沒有寫一行代碼,實(shí)現(xiàn)了一個(gè)簡單的AJAX應(yīng)用,以下為截圖說明。2008-03-03