查看方法
string sql="SELECT * FROM TestFiles WHERE ID = '" + Request.QueryString["ID"] + "'";
SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=;Database=TestUploadFile");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read()){
Response.Clear();
Response.AddHeader("Content-Type",dr["FileType"].ToString());
Response.BinaryWrite((byte[])dr["MyFile"]);
}
dr.Close();
connection.Close();
string sql="SELECT * FROM TestFiles WHERE ID = '" + Request.QueryString["ID"] + "'";
SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=;Database=TestUploadFile");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read()){
Response.Clear();
Response.AddHeader("Content-Type",dr["FileType"].ToString());
Response.BinaryWrite((byte[])dr["MyFile"]);
}
dr.Close();
connection.Close();
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;using System.IO;
using System.Data.SqlClient;public partial class Display : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["Personal"].ConnectionString;
int photoId = Convert.ToInt32(TextBox1.Text );
SqlConnection connection = new SqlConnection(connString); string sql = "SELECT BytesOriginal FROM Photos WHERE PhotoID = "+ @photoId ; SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.Add(new SqlParameter("@photoId", photoId));
connection.Open(); Stream stream = null;
object result = command.ExecuteScalar();
stream = new MemoryStream((byte[])result);
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0)
{
Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}
}