Q: file exists?

  • Thread starter Thread starter Guest
  • Start date Start date
How about:
if (System.IO.File.Exists(Server.MapPath("/Files/myFile.xls"))) {
Response.Redirect("/Files/myFile.xls");
}

Karl
 
Perfect. I have one more problem.
Response.Redirect("/Files/myFile.xls") open files and I see file name in the
address bare. so if user is clever enough they will type and check other file
names. What should I do? (I am using visual basic by the way. not C#.)
 
I think you can configure IIS to not serve up certain file types.
Another solution is to move the files outside the virtual directory.
 
You need to combine one of those solutions with changing to a
Response.WriteFile approach...

Karl
 
If you want to prevent users from seeing other
file names in the Files directory, just open the
IIS Manager, select the directory, and make sure
the "Directory Browsing" property is unchecked.

After you do that, anybody who browses the
/files directory without a filename will get a
"You're not authorized" page.



Juan T. Llibre
ASP.NET MVP
===========
 
Ok. I put my files in a different drive. And changed
Response.Redirect("/Files/myFile.xls") to
Response.WriteFile(“D:\ Files\myFile.xlsâ€)
But I could not see excel file in the browser anymore, lost of garbage? What
is problem?
 
If you look at the example on the microsoft site I provided, or do a little
reasearch, you'll see that the content-type must be set, that the buffer
most likely needs to be cleared and that the response must be ended right
after...

Karl
 
I was trying to find a way to solve the problem without creating a new page.
Here is my new web form and page_load code.

BinaryData.aspx.vb:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim FilePath As String
'Set the appropriate ContentType.
Response.ContentType = "Application/x-msexcel"
'Get the physical path to the file.
FilePath = MapPath("D:\Files\myFile.xls")
'Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath)
Response.End()

End Sub

When I debug it I get this message.
Invalid path for MapPath 'D:\Files\myFile.xls'. A virtual path is expected.
Any idea?
 
You don't need to MapPath if you are going to put the full physical path.

FilePath = "d:\Files\myFile.xls"

I originally had the mapPath in there because you were using a virtual
path...

Karl
 
mmmm. it seems it requires me to add excel file to my project, I have many
files and folders, I can not do that, if that is the requirement, is there
any other way not to show address bar on the screen?
 
Back
Top