Ivonne Aspilcueta

Ivonne Aspilcueta

  • 882
  • 622
  • 10.2k

How to read file to populate image base on values input

Oct 24 2024 7:38 PM

Hello,

I am stucked in this part, there are 2 things that I need to get resolved;

1) I need to populate the whole path as soon as I upload the file. Until now I am hard coding the whole path because when I upload the file does not read the whole path.

2) once I found the image name and extension according to the values I insert in the input text, whether in check numbers or amount or date, retrieve the image view.

Could you help me, please? Before I post it I look in Google and Stackoverflow but I could not find anything nearly similar.

Thank you,

public partial class Conversion : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (!FileUpload1.HasFiles) //Validation
            {
                Response.Write("No file Selected"); return;
            }
            else
            {
                // 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(Path.Combine(filePath));
                //var lines = File.ReadAllLines(filePath);
                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
                        acctNumber = txtacctNumber.ToString();
                        chkNumber = txtchkNumber.ToString();
                        chkAmount = txtchkAmount.ToString();
                        imgDate = txtimgDate.ToString();

                        //Store the imagePath if needed
                        ViewState["ImagePath"] = imagePath;

                        // break; // Remove this if you want to handle multiple lines
                    }

                        // Extract values from textboxes
                        string acctNumbers = txtacctNumber.Value;
                        string chkNumbers = txtchkNumber.Value;
                        string chksAmount = txtchkAmount.Value;
                        string imgDates = txtimgDate.Value;
                        string imagePaths = ViewState["ImagePath"].ToString();

                        // Retrieve the base path from the configuration
                        string basePath = ConfigurationManager.AppSettings["ImageBasePath"];

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

                        //Populate image on website
                    { 
                        Response.Write("Image has been Added");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // Handle any exceptions that occurred
            Console.WriteLine("An error occurred while reading the file: " + ex.Message);
        }
    }
}

 


Answers (3)