• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 705
  • Last Modified:

ASP.NET FileUpload retrieve file path

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.
0
schwientekd
Asked:
schwientekd
  • 4
  • 2
  • 2
1 Solution
 
figrocCommented:
Why would you want to get the absolute file path?
All you should care about is the content, not the path.
0
 
schwientekdAuthor Commented:
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
        Else
            Exit Sub
        End If
        Try
            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
            MyCommand.Fill(DtSet)
            gvCensus.DataSource = DtSet.Tables(0)
            gvCensus.DataBind()
            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

Open in new window

0
 
maheshsnkrCommented:
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
    FileUpload1.PostedFile.SaveAs("~/ServerFolderName/Filename.csv");
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) .. :)
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
figrocCommented:
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.
0
 
schwientekdAuthor Commented:
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.
0
 
maheshsnkrCommented:
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. 
    Directory.CreateDirectory(Server.MapPath("UploadedFiles")) 
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

0
 
schwientekdAuthor Commented:
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).
0
 
schwientekdAuthor Commented:
After some more experimenting I was able to get my solution figure out with the help of this post.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now