使用httphandler从数据库中检索文件名

问题描述:





我有EmpNo(Int)和EmpImage(图像)列的数据库。



我正在使用HttpHandler来显示图像。



我将图像存储在数据库和文件夹中。



现在我想将文件夹中的图像名称更改为EmpNo的名称,我在上传时没有更改。



因此需要从数据库中获取图像名称将它们与文件夹中的图像名称进行比较并重命名。



如何从使用通用数据库从数据库中获取的二进制数据中提取或提取图像名称处理程序。



我已将代码附加到处理程序中以供参考。



Hi,

I have database with EmpNo(Int) and EmpImage(Image) columns.

I am using HttpHandler to display the Images.

I am storing Images both in database and folder.

Now I want to change the names of Images in folder as names of EmpNo whose I didnt change while uploading.

So need to fetch the Images names from database to compare them with the Image names in the folder and rename them.

How can i fetch or extract the image names from the binary data that i get from database using generic handler.

I have attached the code In handler for reference.

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

public class Lab_14___ImageFetchingHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        SqlConnection vConn = new SqlConnection("server=localhost; database=Asp.netDemoWebsiteDatabase; Integrated Security = SSPI;");
        vConn.Open();
        String vQuery = "Select EmpImage from EmpImages where Empno=@id";
        SqlCommand vComm = new SqlCommand(vQuery, vConn);
        
        //Receive the Id from some Form
        String vId = context.Request.QueryString["id"];
        vComm.Parameters.AddWithValue("@id", vId);
        SqlDataReader vDr = vComm.ExecuteReader();
        while (vDr.Read())
        {
            context.Response.ContentType = "image/jpg";
            context.Response.BinaryWrite((byte[])vDr["EmpImage"]);
            
            [ Here I need the Images names to store in List or array.How?? ]
        }
        vConn.Close();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}





我尝试了什么:



我在想如何从SqlDataReader获取文件信息但不知道如何实现它。

是否有任何属性或SqlDataReader中的字段来获取文件的详细信息??



What I have tried:

I am thinking some how I can get the file information from SqlDataReader but no idea how to implement it.
Is there any properties or fields in SqlDataReader to get the details of the file??

如果你没有将图像名称存储在数据库中,你就不能期望SqlDataReader创建一个名字为了你。我可以想到两个选项,但是,第一个选项取决于图像名称是图像元数据的一部分 - 就像EXIF数据一样 - 所以,忽略了......



选项2:更简单的方法 - 假设您有数据库中的员工ID和相关图像,我唯一能想到的就是使用员工ID'命名'图像(可能填充)用'0'来制作相同的长度)并附加.jpg(或你需要的任何图像扩展名)



所以,而不是



If you're not storing the image name in the database you cant magically expect SqlDataReader to invent a name for you. I can think of two options, but, the first of those would depend on the image name being part of the metadata for the image - as in EXIF data - so, ignoring that ...

Option 2 : How about an easier approach - given you have an Employee ID and an associated image from your database, the only thing I can think of is you 'name' the image using the Employee ID (possibly padded with '0's to make the same length) and append ".jpg" (or whatever image extension you need) to that

So, instead of

引用:

[这里我需要图像名称存储在List或array.How ?? ]

[ Here I need the Images names to store in List or array.How?? ]





你使用的东西是





you use something like

// derive name from Employee ID
string imageName = getImageNameFromEmployeeID(vID, ".jpg");





其中getImageNameFromEmployeeID看起来像





where getImageNameFromEmployeeID looks like

string getImageNameFromEmployeeID(int vID, string defaultExtension)
{
   // Pad Employee ID to 10 characters with zeros 
   // Use Simple String append rather than StringBuilder
   // should check defaultExtension not null here etc 

    string tmpImageName = vID.ToString("D10") + defaultExtension;
    return tmpImageName;
}