Ivonne Aspilcueta

Ivonne Aspilcueta

  • 1.1k
  • 610
  • 9k

Getting a filePath from D:\ where my web application is locate in C:\

Jul 16 2024 7:44 PM

Hello,

How can I create a file path from my web application that is locate in C:\ to look for a file and image in D:? to convert image to Bytes and save in database?

//Convert the Image to a Byte Array:
private byte[] ConvertImageToByteArray(string imagePath)
{
    string fullPath = Server.MapPath("~/Images/") + imagePath;
    return File.ReadAllBytes(fullPath);
}

//Save the Data into the Database:
        

protected void Button1_Click(object sender, EventArgs e)
{
    if (!FileUpload1.HasFile) //Validation
    {
        Response.Write("No file Selected"); return;
    }
    else
    {
        // Read and populate textboxes from Index.txt

        string indexFilePath = Server.MapPath("~/index.txt");
     
        PopulateTextBoxes(indexFilePath);

        // Extract values from textboxes
        string acctNumber = txtAcctNumber.Text;
        string chkNumber = txtChkNumber.Text;
        string imgDate = txtDate.Text;
        string imagePath = ViewState["ImagePath"].ToString();

        // Convert the image to byte array
        byte[] imageData = ConvertImageToByteArray(imagePath);

        // Insert the data into the database
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString()))
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO chkImages (acctNumber, chknumber, imgdate, imageData) VALUES (@acctNumber, @chknumber, @imgdate, @imageData)", connection);

            cmd.Parameters.AddWithValue("@acctNumber", acctNumber);
            cmd.Parameters.AddWithValue("@chknumber", chkNumber);
            cmd.Parameters.AddWithValue("@imgdate", imgDate);
            cmd.Parameters.AddWithValue("@imageData", imageData);

            cmd.ExecuteNonQuery();
            connection.Close();

            Response.Write("Image has been Added");
        }
    }
}

 


Answers (2)