schwientekd
asked on
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.
ASKER
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
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.Sav eAs("~/Ser verFolderN ame/Filena me.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) .. :)
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.Sav
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) .. :)
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.
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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).
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).
ASKER
After some more experimenting I was able to get my solution figure out with the help of this post.
All you should care about is the content, not the path.