I need to find out if is there any other way to get the FilePath from drive D:
The code below using Server.MapPath take me to the C: drive per default, but I want to get the D: drive first, then been able to upload any file from D:// drive, my web app is in C: drive.
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"); //Need to change path
//Server.MapPath("~//wcupobproc1-a/Old_Check_Images/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");
}
}
}
I used Server.Mappath, but it is default to C: because my web app is in C: drive.
The point of Server.MapPath
is to convert a virtual path (e.g. a URL to a location in your web site) to a physical path. It sounds like your app is located on the c: drive. If this is the case, MapPath will only return locations on your c: drive.
If you want to be able to specify an arbitrary filesystem location that is not related to a virtual path, you can either hardcode the drive letter in your code or define the path in your web.config as an AppSetting and then retrieve it in code.