ASP.NET FileUpload retrieve file path

Posted on 2009-12-28
Last Modified: 2013-11-26
I am building an aspx page in visual studio 2008 and using the FileUpload control to select a CSV file to import to sql.  I have everything figured out except trying to retrieve the file folder path that is selected by the user.  I can make the scenario work by retrieving the complete file path if it is an excel file but I guess it is a different story when importing a CSV file.
Question by:schwientekd
    LVL 2

    Expert Comment

    Why would you want to get the absolute file path?
    All you should care about is the content, not the path.

    Author Comment

    From what I understand I have to specify the CSV filename in the query and then specify the file location separately.  I've listed my code.  Can you be more specific about how you would approach this?
            If btnBrowse.HasFile = True Then
                stFilePathAndName = btnBrowse.PostedFile.FileName.ToString()
                fileName = btnBrowse.FileName
                Exit Sub
            End If
                Dim MyConnection As System.Data.OleDb.OleDbConnection
                Dim DtSet As System.Data.DataSet
                Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
                MyConnection = New System.Data.OleDb.OleDbConnection _
                ("provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\; Extended Properties=""Text;HDR=No;FMT=Delimited""")
                MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" & fileName & "]", MyConnection)
                MyCommand.TableMappings.Add("Table", "TestTable")
                DtSet = New System.Data.DataSet
                gvCensus.DataSource = DtSet.Tables(0)
            Catch ex As Exception
            End Try

    Open in new window

    LVL 4

    Expert Comment

    hi schwientekd,

    As per my understanding, I suppose you are not saving the file (selected by the user) in the server.
    You are just mapping the path of the uploaded file to read the contents.
    If so, you need to change the logic to such a way that...

    1. Select the CSV file using the FileUpload Control.
    2. Save the file in a server folder using
    3. Now you know the File Saved Path and from there you can find the file and read its contents.

    If this is the thing you are looking for, please dont hesitate to ask me (if needed) .. :)
    LVL 2

    Expert Comment

    maheshsnkr is right. The aspx page is running at server side. It operates upon the file persists in the server, not the file in the client, which is a copy of the file selected by user.

    Author Comment

    Is there an alternative to my approach here?  I can make this work in my vb application but the web side doesn't seem to have a solution, which is what I need.
    LVL 4

    Accepted Solution

    hi schwientekd,

    Can you try the code which is attached.

    Steps which are done
    1. Get the file from the FileUpload control.
    2. Check for the existance of the ServerFolder 'UploadedFiles'
    3. If it doesnot exists, Create one.
    4. Save the file inside that folder (with the same file name as of the uploaded file)
    5. Assign the new file path in a variable (FileSavedPath), which can be used to read the contents.
    Dim FileSavedPath As String = String.Empty 
    'Check whether a folder exists in the server-side or not. 
    'Imports System.IO namespace 
    Dim DirInfo As New DirectoryInfo(Server.MapPath("UploadedFiles")) 
    If DirInfo.Exists = False Then 
        'If folder is not there, Create one. 
    End If 
    If FileUpload1.PostedFile IsNot Nothing Then 
        FileUpload1.PostedFile.SaveAs(Server.MapPath(ResolveUrl("~/UploadedFiles/" & FileUpload1.FileName))) 
    End If 
    'Instead of 'fileName', you can use 'FileSavedPath'. 
    FileSavedPath = Server.MapPath(ResolveUrl("~/UploadedFiles/" & FileUpload1.FileName))

    Open in new window


    Author Comment

    Thanks for the sample code but it doesn't work.  I get an error saying the ms jet database engine cannot find the object 'c:\...path to file'.  The file does upload successfully to the path it specifies in the error message though.

    From what I understand you cannot open up a CSV file by using the full path.  It works for .xls files but not CSV.  You need to first specify the folder path to the file (Data Source = folderpath;...) and then specify the name of the file in the query (select * from [filename], conn).

    Author Closing Comment

    After some more experimenting I was able to get my solution figure out with the help of this post.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now