Last post Jul 01, 2016 08:33 PM by NithinB
May 28, 2016 06:42 PM|NithinB|LINK
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 didn't 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.
public class Lab_14___ImageFetchingHandler : IHttpHandler
public void ProcessRequest(HttpContext context)
SqlConnection vConn = new SqlConnection("server=localhost; database=Asp.netDemoWebsiteDatabase; Integrated Security = SSPI;");
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"];
SqlDataReader vDr = vComm.ExecuteReader();
context.Response.ContentType = "image/jpg";
context.Response.BinaryWrite((byte)vDr["EmpImage"]); [ Here I need the Images names to store in List or array.How?? ]
public bool IsReusable
Jun 05, 2016 09:03 PM|NithinB|LINK
Still waiting for the answers.
Jun 21, 2016 09:24 AM|PatriceSc|LINK
It's a bit unclear so hard to answer. You can't directly retrieve a file name from just its binary content. My understanding is that on one side you have just an internal id and the content and on the other side you have the file stored somewhere (in a
single folder ?) And that for now you have no way to relate a given file to the corresponding db row (or do you ?)
So it seems that the only thing you could do for now would be to compare the file content and the db content either directly or by going through a hash value to match each db row to the corresponding file.
Jun 21, 2016 07:41 PM|NithinB|LINK
Let me make it simple. I have stored images in binary form in db. Now I am displaying them using HttpHandlers as the code i gave. All I need is to get the file name. That is the main issue. If u got any procedure to get it done., please share the code with
Jun 21, 2016 08:10 PM|PatriceSc|LINK
So it seems really a design issue. I would rather :
- add a new column in the db to store the file name
- would create a console app that would browse each file, would read the content (possibly using System.IO.File.ReadAllBytes), would select rows having the same length (DATALENGTH if using SQL Server and then the row with the exact same content (by comparing
two byte arrays) before filling the column with the single row that is finally found
- solve possibly remaining issues by hand (two files having the same content)
At this step the file name can be retrieved from the db at the same time than its content. Files are not needed any more and could be deleted (you'll always be able to create them as you have both the content and the file name in the db).
Ah and change your upload process to save both the content and the name to the db.
For now with your design it seems you have unrelated files and db data and there is just no way to easily relate a file name stored on the disk (one again the name of a file is not part of its content so if you have just an integer id and the content for
each file and files stored on disk there is no way to really relate a db row to the corresponding file stored on disk (if I understood correctly your current design).
Jul 01, 2016 08:33 PM|NithinB|LINK
OK. this makes sense as file name is not the part of the file we cant get the name of the file.
If that is right then Name of file should be other part. Means Name part + file part = Some other part(not file part). Which means are we using wrong name as File instead of some other name.
Just trying to think logically not joking.