Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.xls file as linked server 1

Status
Not open for further replies.

lanm

Programmer
Jul 7, 2005
244
US
I've setup, through DTS, my linked server as an .xls file.

What I want to do is have a .aspx page which allows the user to upload a .xls file to the following database server directory:
C:\TestSaving\

I've got the code to read the file once it's in the directory and select all the rows from the .xls file and insert them into a table, but I can't get the file to save into the database server directory above. I had to hand create it to get the whole update process to work.

Here's my code:

***************

Dim getmyFile As HttpPostedFile = myfile.PostedFile
Label2.Text = "Line 76"
If IsNothing(getmyFile) Then
Label2.Text = "Please select a file to upload"
Else
If getmyFile.ContentLength = 0 Then
Label2.Text = "Cannot upload zero length File"
Else
Dim ServerFileName As String = Path.GetFileName(myfile.PostedFile.FileName)
Server.MapPath("//db2")
getmyFile.SaveAs("C:\TestSaving\" & ServerFileName)
Label2.Text = "Successful upload to C:\TestSaving\" & ServerFileName

****************

Any suggestions are welcome!

Thanks!
 
I generally use something like:
Code:
Dim strFile as string = System.IO.Path.GetFileName(fileInputID.PostedFile.FileName)
If FileFieldSelected(fileUpload) = True Then
   fileInputID.PostedFile.SaveAs(Server.MapPath("FolderName") & "\" & strFile)
Else
   ' Handle Error
End If

and the FileFieldSelected function to check if the file is valid an not over a certain size:

Code:
    Public Function FileFieldSelected(ByVal FileField As System.Web.UI.HtmlControls.HtmlInputFile) As Boolean
        ' Returns a True if the passed
        ' FileField has had a user post a file
        Dim intFileLength As Integer
        If FileField.PostedFile Is Nothing Then Return False
        If FileField.PostedFile.ContentLength = 0 Then Return False
        If FileField.PostedFile.ContentLength > 5120000 Then Return False
        Return True
    End Function


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm,

How can I ensure the file is only an .xls file?
I vaguely remember how to do it in VB6.

Thanks!
 
You could use the Extension property in System.IO.FileInfo or you could simply split the filename string (by a ".") and take the string after the last instance of the "."

Either way is fine although it won't actually guarantee that the file is actully an excel file (i.e. if someone renamed "myfile.exe" to "myfile.xls" it would still be allowed through).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I think you can also add a accept tag to the input element as well e.g. (accept="*.xls")


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm,

I didn't see an "accept=" in the inteli-sense for the input tag. Could it be something else?

Thanks for the help!
 
Hmmm...that must now be a property that is no longer supported. I found the following at which suggested that it was supported at one time (have a look at the "File Attachments (type=file)" section ):



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
bummer...yeah, I see it. Thanks!

I tried to put that in and get:
Can't find attribute 'accept' of element 'input'.
 
Even Microsoft have it on their site:


although it does say that it accepts MIME types and not file extensions so the example I gave you above would be incorrect.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top