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);
}
}
}
Ivonne AspilcuetaPosted Oct 25, 2024, 6:05 PM
Hi Gowtham,
I added the FileUpload1.PostedFile.FileName, but when debugging the code erros in this line:
Also if Fileupload1.PostedFile.Filename is giving the whole path, i won't be need it this:
****string path = ConfigurationManager.AppSettings["ImageBasePath"];
Gowtham CpPosted Oct 25, 2024, 8:49 AM
Hello Ivonne Aspilcueta ,
To dynamically populate the file path upon uploading and retrieve the corresponding image based on user input, modify your code to use
FileUpload1.PostedFile.FileNamefor the full path and compare user input values with those extracted fromindex.txt. If a match is found, construct the full image path and set it to anImagecontrol'sImageUrlproperty for display.Hope it helps!
Asma KhalidPosted Oct 24, 2024, 10:31 PM
Dear Ivonne,
Uploading files in ASP.NET works differently than WPF or desktop based application events. You can not hard code file path since its URL based. Everytime you upload a file, the file contnet or extension is captured within the HTTP request packet. Your system then extract the content from HTTP request packet and placed it into publicly accessible folder normally "Content" so, you can later access the file via URL. You have the authority to add additional folder and file name into file path at runtime. There are two ways you can save the uploaded file on the server i.e. either as a file (while storing file path like http://www.mysite.com/contnet/myfile.png to your database) OR as a binary content directly on your database. I recommend you to look into below articles i.e.
1. Upload/Download file to/from web server -> https://bit.ly/2XVaXcb
2. Upload File as File on server -> https://bit.ly/2BySQM9
3. Upload File into Database on server -> https://bit.ly/2AoN6DZ
I hope this helps and clarifies a lot of things.
Thanks & Regards,
Asma Khalid