Здравствуйте, возникла следующая проблема. При использовании HttpHandler не отображаются изображения на странице.
Код хэндлера вот:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
namespace AddDoctor
{
/// <summary>
/// Сводное описание для Handler1
/// </summary>
public class PictureHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest req = context.Request;
HttpResponse resp = context.Response;
Int32 id = Int32.Parse(req.QueryString["id"]);
byte[] imgData;
SqlConnection conn = new SqlConnection(@"Data Source = .\SQLEXPRESS; Initial catalog = Umbraco; Trusted_connection=true");
SqlCommand cmd = new SqlCommand("SELECT img_content FROM Images WHERE id_img=@id",conn);
cmd.Parameters.AddWithValue("@id", id);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
resp.ContentType = reader["img_type"].ToString();
if (reader.Read())
{
imgData = (byte[])reader["img_content"];
resp.BinaryWrite(imgData);
}
resp.End();
reader.Close();
conn.Close();
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
в web.config добавляю такую строчку
1.
<add verb="*" path="umbraco/data/Handler1.ashx" type="AddDoctor.PictureHandler, PictureHandler"/>ctureHandler"/>
в Razor скрипте прописываю следующее:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
var reader1 = umbraco.BusinessLogic.Application.SqlHelper.ExecuteReader("select * from Images");
while (reader1.Read())
{
<ul>
<li>
<img src="Handler1.ashx?id=@reader1.GetInt("id_img")" />
</li>
</ul>
}
reader1.Close();
в итоге на странице сайта выводится вот такая фигня
изображений в базе 4 штуки
на всякий случай вот код загрузки изображений в базу с помощью FileUpload
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
string fileName = Path.GetFileName(fileUp.PostedFile.FileName);
fileUp.PostedFile.SaveAs(HostingEnvironment.ApplicationPhysicalPath + fileName);
txtResult.Text = String.Format("Был загружен файл с именем {0} и размером {1} Кбайт.", fileName, fileUp.PostedFile.ContentLength/1024); ;
byte[] myimage = new byte[fileUp.PostedFile.ContentLength];
HttpPostedFile Image = fileUp.PostedFile;
Image.InputStream.Read(myimage, 0, (int)fileUp.PostedFile.ContentLength);
SqlConnection myConnection = new SqlConnection(@"Data Source = .\SQLEXPRESS; Initial catalog = Umbraco; Trusted_connection=true");
SqlCommand storeimage = new SqlCommand("INSERT INTO Images "
+ "(img_content, img_type, img_size, img_title) "
+ " values (@image, @imagetype, @imagesize, @imagetitle)", myConnection);
storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage;
storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 10).Value = fileUp.PostedFile.ContentType;
storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = fileUp.PostedFile.ContentLength;
storeimage.Parameters.Add("@imagetitle", SqlDbType.VarChar, 100).Value = title.Text+Path.GetExtension(fileUp.FileName);
myConnection.Open();
storeimage.ExecuteNonQuery();
myConnection.Close();
Заранее спасибо.