PDA

View Full Version : سوال: درخواست یک هندلر کلی برای انواع فایل



moferferi
یک شنبه 15 مرداد 1391, 22:51 عصر
سلام.
من توی برنامم از Dynamic Web TWAIN برای اسکن تصاویر استفاده میکنم
توی این برنامه کاربر میتونه فایل اسکن شده را با فرمت های مختلفی مثل
jpg .bmp ,gif ,tif, multi tif ,pdf ,png
توی دیتابیس به صورت باینری ذخیره کنه.
حالا میخوام برای نمایش فایل ها یه هندلر داشته باشم که بتونه تمامی انواع فایل را ساپورت کنه و مجبور نباشم مثلا واسه jpg بیام یه هندلر جدا و برای pdf یه هندلر دیگه بنویسم.

و بر فرض اینکه بیام و چند تا هندلر که توی هر کدوم ContentType های مختلفی تعریف کنم ،مشکل اینه که نمیدونم اون فایل ذخیره شده از چه نوعیه و چه هندلری را صدا بزنم.

این هندلری هستش که ازش استفاده میکنم
<%@ WebHandler Language="C#‎" Class="FileHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class FileHandler : IHttpHandler {

const string conString = @"Data Source=A;Initial Catalog=personalpic;Integrated Security=True";

public void ProcessRequest (HttpContext context) {

context.Response.ContentType = "image/jpg";

SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("SELECT FileScan FROM ScanFiles WHERE Id=@Id", con);
cmd.Parameters.AddWithValue("@Id", context.Request["Id"]);
using (con)
{
con.Open();
byte[] file = (byte[])cmd.ExecuteScalar();
context.Response.BinaryWrite(file);
}
}

public bool IsReusable {
get {
return false;
}
}

}
البته در حال حاضر چند تا هندلر با ContentType های مختلف تعریف کردم.
ولی همون طور که گفتم نمیدونم کاربرفایل اسکن شده را با چه فرمتی توی دیتابیس ذخیره میکنه تا من هم هندلر مخصوص خودش را صدا بزنم.

با تشکر

fakhravari
یک شنبه 15 مرداد 1391, 23:52 عصر
این کلاس اضافه کنید.
public static class ImageHelper
{
public static ImageFormat GetContentType(byte[] imageBytes)
{
MemoryStream ms = new MemoryStream(imageBytes);

using (BinaryReader br = new BinaryReader(ms))
{
int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;

byte[] magicBytes = new byte[maxMagicBytesLength];

for (int i = 0; i < maxMagicBytesLength; i += 1)
{
magicBytes[i] = br.ReadByte();

foreach (var kvPair in imageFormatDecoders)
{
if (magicBytes.StartsWith(kvPair.Key))
{
return kvPair.Value;
}
}
}

throw new ArgumentException("Could not recognise image format", "binaryReader");
}
}

private static bool StartsWith(this byte[] thisBytes, byte[] thatBytes)
{
for (int i = 0; i < thatBytes.Length; i += 1)
{
if (thisBytes[i] != thatBytes[i])
{
return false;
}
}
return true;
}

private static Dictionary<byte[], ImageFormat> imageFormatDecoders = new Dictionary<byte[], ImageFormat>()
{
{ new byte[]{ 0x42, 0x4D }, ImageFormat.Bmp},
{ new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, ImageFormat.Gif },
{ new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, ImageFormat.Gif },
{ new byte[]{ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, ImageFormat.Png },
{ new byte[]{ 0xff, 0xd8 }, ImageFormat.Jpeg },
};

}
برای گرفتن
ImageFormat contentType = ImageHelper.GetContentType(ورودی byte[]);
string dd = contentType.ToString();

fakhravari
یک شنبه 15 مرداد 1391, 23:59 عصر
("JPG", new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 });

("JPEG", new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 });

("PNG", new byte[] { 0x89, 0x50, 0x4E, 0x47 });

("TIF", new byte[] { 0x49, 0x49, 0x2A, 0x00 });

("TIFF", new byte[] { 0x49, 0x49, 0x2A, 0x00 });

("GIF", new byte[] { 0x47, 0x49, 0x46, 0x38 });

("BMP", new byte[] { 0x42, 0x4D });

("ICO", new byte[] { 0x00, 0x00, 0x01, 0x00 });

moferferi
دوشنبه 16 مرداد 1391, 21:22 عصر
سلام.
خیلی ممنون از لطفتون
من کلاس را به برنامه اضافه کردم
اما کد
رImageFormat contentType = ImageHelper.GetContentType(ورودی byte[]);
string dd = contentType.ToString();
کجا بزارم.؟توی هندلر ؟
و اینکه میشه کاری کرد که فایل های pdf , word را هم ساپورت کنه؟

fakhravari
دوشنبه 16 مرداد 1391, 21:33 عصر
برای مثال
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Image_Varbinary
{
public class ShowImages : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["id"] == null) return;

string autoId = context.Request.QueryString["id"];
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationMa nager.ConnectionStrings["Database1ConnectionString1"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT img FROM Table1 where id=@id", conn))
{
cmd.Parameters.Add(new SqlParameter("@id", autoId));
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection) )
{
reader.Read();

byte[] Ax = (Byte[])reader[reader.GetOrdinal("img")];

ImageFormat contentType = ImageHelper.GetContentType(Ax);

context.Response.BinaryWrite(Ax);
context.Response.ContentType = contentType.ToString();
reader.Close();
}
}
}
}


public bool IsReusable
{
get
{
return false;
}
}
}
}


برای فایل های word , pdf هم باید نوع byte باشه.

moferferi
دوشنبه 16 مرداد 1391, 22:00 عصر
بازهم ممنون
همه فرمت ها درست نشون داده میشه به غیر از tiff
من اخر کلاس کد
{ new byte[]{ 0x49, 0x49, 0x2A, 0x00 }, ImageFormat.Tiff},
برای ساپورت فایل های tiff اضافه کردم.
اما موقع نمایش خطای
XML Parsing Error: syntax error
Location: http://localhost/Kargozini/FileHandlerjpg.ashx?id=12
Line Number 1, Column 1:
II*
میده
اینم هندلر جدید
<%@ WebHandler Language="C#" Class="FileHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class FileHandler : IHttpHandler {
const string conString = @"Data Source=A;Initial Catalog=personalpic;Integrated Security=True";
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/jpg";
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("SELECT FileScan FROM ScanFiles WHERE Id=@Id", con);
cmd.Parameters.AddWithValue("@Id", context.Request["Id"]);
using (con)
{
con.Open();
byte[] file = (byte[])cmd.ExecuteScalar();
System.Drawing.Imaging.ImageFormat contentType = ImageHelper.GetContentType(file);
context.Response.BinaryWrite(file);
context.Response.ContentType = contentType.ToString();
}
}
public bool IsReusable {
get {
return false;
}
}
}

fakhravari
دوشنبه 16 مرداد 1391, 22:31 عصر
کلا باید همه فرمتها byte گیر بیاد سرچ کنید.
:چشمک: