using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
namespace PictureHandler
{
public class PictureHandler : IHttpHandler
{
bool IHttpHandler.IsReusable
{
get { return true; }
}
void IHttpHandler.ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
SqlConnection connection1 = new SqlConnection(GetConnectionString());
SqlCommand myCmd = new SqlCommand("SELECT Picture from im where id = @id", connection1);
myCmd.Parameters.Add("@id", empID);
connection1.Open();
SqlDataReader rdr = myCmd.ExecuteReader();
rdr.Read();
Response.ContentType = "image/jpeg";
Response.OutputStream.Write(rdr.GetSqlBinary(0).Value, 0, rdr.GetSqlBinary(0).Length);
Response.End();
rdr.Close();
connection1.Close();
[color=#FF0000]это конечно нада вставлять в .aspx но где и как чего-то я не въехал
//<img src="photo.aspx?id=10">[/color]
}
}
public partial class Hello : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
}
// Processes click on our cmdSend button
private void cmdSend_Click(object sender, System.EventArgs e)
{
// Check to see if file was uploaded "Проверить, что адрес не пуст"
if (filMyFile.PostedFile != null)
{
// Get a reference to PostedFile object "Получить ссылку на объект"
HttpPostedFile myFile = filMyFile.PostedFile;
// Get size of uploaded file "Получить размер файла"
int nFileLen = myFile.ContentLength;
// make sure the size of the file is > 0 "Удостоверится, что размер файла > 0"
if (nFileLen > 0)
{
// Allocate a buffer for reading of the file "Подготовить буфер для чтения файла"
byte[] myData = new byte[nFileLen];
// Read uploaded file from the Stream "Прочитать загруженый файл из потока"
myFile.InputStream.Read(myData, 0, nFileLen);
// Create a name for the file to store "Создать название файла для того, чтобы хранить"
string strFilename = Path.GetFileName(myFile.FileName);
// Write data into a file "Записать данные в файл"
WriteToFile(Server.MapPath(strFilename), ref myData);
// Set label's text "Текст ярлыка"
lblInfo.Text =
"Filename: " + strFilename + "<br>" +
"Size: " + nFileLen.ToString() + "<p>";
// Set URL of the the object to point to the file we've just saved
// "Передать путь файла который только что записали"
imgFile.ImageUrl = strFilename;
imgFile.ToolTip = "This file was stored to as file.";
lblText1.Text = imgFile.ImageUrl;
// show the images and text "Показать рисунок и текст"
imgFile.Visible = true;
//imgDB.Visible = true;
lblText1.Visible = true;
//lblText2.Visible = true;
// Writes file to the database
//==============================
// Create a byte[] from the input file
int len = filMyFile.PostedFile.ContentLength;
byte[] pic = new byte[len];
filMyFile.PostedFile.InputStream.Read(pic, 0, len);
// Insert the image and comment into the database
SqlConnection connection = new SqlConnection(GetConnectionString());
try
{
connection.Open();
SqlCommand cmd = new SqlCommand("insert into im " + "(Picture, Comment) values (@pic, @text)\n SELECT SCOPE_IDENTITY()", connection);
cmd.Parameters.Add("@pic", pic);
cmd.Parameters.Add("@text", Comment.Text);
decimal id = (decimal)cmd.ExecuteScalar();
string ura = Convert.ToString(id);
TextBox1.Text = ura;
}
finally
{
connection.Close();
}
}
}
}
// Writes file to current folder "Записать файл в текущую папку"
private void WriteToFile(string strPath, ref byte[] Buffer)
{
// Create a file "Создать файл"
FileStream newFile = new FileStream(strPath, FileMode.Create);
// Write data to the file "Записать данные к файлу"
newFile.Write(Buffer, 0, Buffer.Length);
// Close file "Закрыть файл"
newFile.Close();
}
// Generates database connection string
private string GetConnectionString()
{
return "Data Source=localhost;Initial Catalog=MayBase;Integrated Security=True";
}
// Reads the name of current web page
private string GetMyName()
{
// Get the script name
string strScript = Request.ServerVariables["SCRIPT_NAME"];
// Get position of last slash
int nPos = strScript.LastIndexOf("/");
// Get everything after slash
if (nPos > -1)
strScript = strScript.Substring(nPos + 1);
return strScript;
}
}
}