Based on your description, I'm guessing that the preceding code simply takes the path of the file from the file upload control, and doesn't attempt to save it anywhere on the server.
The path provided by the file upload control is the path of the file
on the client.
(Most browsers will even strip out the folder path, and simply send the file name.)
The file has not been saved in this path on the server. If it was, it would be a major security vulnerability, since attackers could overwrite any file on your server simply by manipulating the path.
Instead, you either need to use the
SaveAs[
^] method to save the file to a specific folder on the server, or use either the
FileContent[
^] or
FileBytes[
^] properties to access the raw content of the file.
string directory = Path.GetTempPath();
string fileExtension = Path.GetExtension(uploadExcelFile.FileName);
string fileName = Path.Combine(directory, Guid.NewGuid().ToString("N") + fileExtension);
string connection;
if (string.Equals(fileExtension, ".xls", StringComparison.OrdinalIgnoreCase))
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + fileName + "'; Extended Properties='Excel 8.0;HDR=YES;'";
}
else if (string.Equals(fileExtension, ".xlsx", StringComparison.OrdinalIgnoreCase))
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0 Xml;IMEX=1;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text;'";
}
else
{
throw new InvalidOperationException("Invalid file type");
}
uploadExcelFile.SaveAs(fileName);
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
...