File Field

  • Thread starter Thread starter Eddy
  • Start date Start date
E

Eddy

I am coding a ASP to process a excel file and output the
reults to a text file. To browse for the file that i want
to process i used the File field to return the location of
the file. My problem begins here when I click on the
browse it show me my harddrive and I slect the file and it
show up in the window "C:\test1.xls". however when I press
the button to process the it returns an error telling me
that the file cannot be found. I did a little testing and
found out that even though I am selecting the file from my
hardrive it is looking for it in the c drive of the server
Same thing happen when I write the text file. it save it
to the server not to my PC
Any help would be appreciated
Thanks
 
An HTML "input type=File" form field is used for UPLOADING files TO the
server, not downloading files FROM the server. Therefore, when you click the
Browse button, it shows the files on the local client machine. There is
nothing that can be done to change this. To browse files on the server for
DOWNloading, you would need to write an ASPX page that shows the files in a
given directory, with a hyperlink on each file name to download the file.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.
 
The file must first be uploaded to the server in order for the script to
process it.
Security restricts access to client side files.

I suggest you break this into a few steps:
1) Pick the file from the client PC
2) Upload to the server
3) Server processes it and saves a text file to the Server.
4) User gets a link presented to them so they can then download the file to
their PC.

hope that helps,
-fs
 
Bah!

There is a fundamental misunderstanding at work here, Eddy.

Your browse control just gives a String, say c:\myfile to your server. Your
server is using that to try and load a file. However, that file is not on
your server. Like Francis said, you need to GET THE FILE TO THE SERVER
before you can load it at your server.

So follows the steps Francis set out and you may get somewhere.

HTH,

Tom
 
ok, I want my program to read this excel file that i have
on my local PC, when i click browse on my app , it show me
the open dialog box and I select the file that is on my
local PC, then I click ok and the path to the file is
diaplayed on the File field "c:\test1.xls"
when I click on the process file , my code should read
that file from my local PC however, the program ends up
looking at the c: drive on the web server so of course it
tell me that it cannot find the file. How do i tell it the
path diaplayed is on my local PC and not on the web server
 
Eddy, you can't do that.
The web server can only access IT's disk.
The reason is security.
If a web server could access the disk of all those who browse it, there'd be
chaos.
Sorry, see my earlier email for the steps you should follow.
Hope that helps,
regards,
-Francis Shanahan
 
I've got the same question as Eddy. How do you go about uploading the
file from the local pc to the Web Server. We have the 'file' type input
control that shows the client side location of the file after browsing
to it. When clicking submit, how do you go about pulling/reading the
file in from the client pc? THAT is what eddy as well as myself are
asking.
 
It's way too easy...

Create an "upload" button. When the user clicks it, fire this code:

If Not (fileFiled.PostedFile Is Nothing) Then
Try
' This actually uploads the file and saves it on the server
fileField.PostedFile.SaveAs("c:\folder\file.txt") ' Any path
you want here. Make sure you have write access.

Catch exc As Exception
' error handling code here.

End Try
End If

Good Luck,

Chris

Christopher Campbell
mailto:[email protected]
 
Also look here
(ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfSystemWebUIHtmlControlsHtm
lInputFileClassPostedFileTopic.htm)

in the framework docs
 
Clint,
this is surprisingly easy with asp.net
Here's an article describing the entire process
http://www.ondotnet.com/pub/a/dotnet/2002/04/01/asp.html
and here's the code I myself have used and know works:

Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Submit1.ServerClick

If Not File1.PostedFile Is Nothing And
File1.PostedFile.ContentLength > 0 Then
Try
Dim fn As String =
System.IO.Path.GetFileName(File1.PostedFile.FileName)
Dim SaveLocation As String = "c:\inetpub\wwwroot"

File1.PostedFile.SaveAs(SaveLocation)
Response.Write("The file has been uploaded.")
Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
End Try
Else
Response.Write("Please select a file to upload.")
End If
End Sub

regards.
-Francis Shanahan
 
Back
Top