Read text file when using an input text to display images

ivonsurf123

New member
Joined
Sep 13, 2024
Messages
1
Programming Experience
Beginner
Hello,

How can I use textboxes to insert information from a text file locate in C:\ to populate the image on website, so far, I prepare the basic, but I am not sure how to do the rest.
Thank you.

This is the code so far:

C#:
{
    public class ImageHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {

            string FileUpload1 = context.Request.QueryString["FileUpload1"];
            try
            {
                if (FileUpload1 == null) // Validation
                {
                    context.Response.Write("No file Selected");
                    return;
                }
                else
                {

                    //Code Here:

                    // Read and Extract Information from the Index.txt File:
                    string path = ConfigurationManager.AppSettings["ImageBasePath"];
                    var files = Directory.GetFiles(path, "index.txt", SearchOption.AllDirectories);

                    // Read and populate textboxes from Index.txt
                    string filePath = Path.Combine(files);

                    var lines = File.ReadAllLines(filePath);
                    List<string[]> parsedLines = new List<string[]>();

                    // Parse lines and prepare data
                    foreach (var line in lines)
                    {
                        var parts = line.Split('|');
                        if (parts.Length >= 9)
                            {
                                // Extract required information
                                string acctNumber = parts[0];
                                string chkNumber = parts[1];
                                string chkAmount = parts[2];
                                string imgDate = parts[5];
                                string imagePath = parts[8];

                            // Populate the textboxes 


                            // Construct the full path to the image file
                            string fullImagePath = Path.Combine(path, imagePath);

                            // Populate the image from imagePath 

                        }
                    }
                                             
                }
            }
            catch (Exception ex)
            {
                // Handle any exceptions that occurred
                Console.WriteLine("An error occurred while reading the file: " + ex.Message);
            }
        }
 
You should not be using the contents of text boxes to populate the contents of images. You should be using the file path you have on line 47 to create a URL that you can them use as the src attribute of an <image> element in your HTML in your HTTP response .

Did you really prepare anything for us, or was that something that your instructor had setup for you? Personally, I wouldn't be using something as low level as a IHttpHandler as a teaching tool unless my objective is to teach students how to do things from the ground up and not be dependent on ASP.NET, WebForms, MVC, or Razor pages.

Furthermore, the code presented there is confusing. It seems like the premade code is to try to handle an image upload attempt, but the code seems to be trying to return a page that shows the check images that are already in the system.
 
Last edited:

Latest posts

Back
Top Bottom