Question How to specify the path in File Upload Control?

Anonymous

Well-known member
Joined
Sep 29, 2020
Messages
94
Programming Experience
Beginner
Hello,

I am trying to upload an excel file into SQL server. The thing is I am stuck here

C#:
  string fileName = Path.Combine(@"C:\", FileUpload1.FileName);
                    FileUpload1.SaveAs(fileName);

                    String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", fileName);

The line
C#:
string fileName = Path.Combine(@"C:\", FileUpload1.FileName);
gives pathname as C:\\FileName for which I get an exception that access is denied, and from this Why is access to the path denied?
I got to know that we can get these errors if the path is a directory. I know my path is wrong, but I am clueless as how to select the correct path for further processing.

My asp.net code is

C#:
<asp:FileUpload ID="FileUpload1" runat="server" Width="368px" />
        <p>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </p>

When I referred this
and changed my code to
C#:
string path = string.Concat(Server.MapPath("~/UploadFile/" + FileUpload1.FileName));
                    FileUpload1.SaveAs(path);

I got Could not find a part of the path exception.
C#:
+        $exception    {"Could not find a part of the path 'C:\\Users\\AMEN\\source\\repos\\ExcelDatabase\\ExcelDatabase\\Files\\New Microsoft Excel Worksheet.xlsx'."}    System.IO.DirectoryNotFoundException
 
Last edited:
Have you trying using Server.MapPath() to convert physical path to virtual, I notice that you're using Webform most of the time IIS does not like windows physical path that is why you must convert to virtual. Good luck
 
Last edited by a moderator:
Look closely, he already is using Server.MapPath(). See the part where he says "and change my code to".
 

Latest posts

Back
Top Bottom