Click to See Complete Forum and Search --> : [RESOLVED] access files from a different drive on the same server
vbnov
January 9th, 2008, 02:49 PM
Hi all
I am trying to access a file which is on the web server but on a different drive E:. Everything works fine from my local pc and on the local network, but when I tried to access it from outside my network I got error.
The error message is " Cannot find 'file:///E:/xy%20-z/2008/asd%20def.pdf'. Make sure the path or Internet address is correct"
Any help would be greatly appreciated
Yeorwned
January 9th, 2008, 02:58 PM
Is your browser throwing the error or the .net application?
vbnov
January 9th, 2008, 03:05 PM
error message is from IE
Yeorwned
January 9th, 2008, 03:12 PM
It is literally trying to get the file from E:/xy%20-z/2008/asd%20def.pdf on the machine which you are using. If I went to your site and clicked on that pdf, it would try to open my E: drive and go to that directory, which would error out.
It sounds like you made a hyperlink to the file in your application rather than mapping the directory and having IIS recast the file structure. To test this, move your mouse over the link to the file you want to download, but don't click on it. If using Internet Explorer, look in the status bar (lower left) and see if the "file://E:/xy%20-z/2008/asd%20def.pdf" shows up or if it is "http://yoursite/asd%20def.pdf" which shows up. If it starts with file://, it will only work on the local server.
To do you what you attempting, the file needs to be in the same directory as the web application unless you have the .net application get the file from E: and send it to the client. In fact, it should not work on your local network either unless the workstation you are using has an E: drive with the file in the same directory.
vbnov
January 9th, 2008, 03:20 PM
Thanks a lot for the quick response. I guess You are right. I have mapped the E: from the webserver on my machine.
Would you please shed some light on how I can map the drive and have IIS recast the file using my .net application?
Thanks again.
Yeorwned
January 9th, 2008, 03:50 PM
I guess I'd list the files out on one page and use another to retrieve them via GET or POST. You will have to get permissions to the files so that IIS can reach them.
Dim url As String = "E:\xy%20-z\2008"
Dim file As String
Dim files() As String = Directory.GetFiles(url)
Dim objStringBuilder As New StringBuilder(2048)
For Each file In files
Dim filepath As String = Path.GetFileName(file)
objStringBuilder.Append("<a href=POSTBACKPAGE.aspx?selectedfile=")
objStringBuilder.Append(url).Append(Server.UrlEncode(filepath))
objStringBuilder.Append(">").Append(filepath).Append("</a><br />")
Next
ASPLABEL.Text = objStringBuilder.ToString()
This assumes you create a new page (POSTBACKPAGE) to take the GET statement passed to it and that you used a label for link display. Now you have to create a page to handle the data sent...
Dim path As String = "E:\xy%20-z\2008"
Dim selectedfile As String = Request.Params("selectedfile")
If File.Exists(selectedfile) And filepath.StartsWith(path) Then
Dim getfile As String = Path.GetFileName(selectedfile)
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; getfile=""" & getfile & """")
Response.Flush()
Response.WriteFile(selectedfile)
End If
That should handle most all file types since it is in octet-stream mode to browser. Any file within your path is able to be downloaded if someone played a guessing game. If you want to fix that, you can either take permissions off or get really fancy with a comparison data file.
The easiest way would to be just creating another web site on the same machine that either acts as a subdomain or simply runs on a different port. You can then set its default directory to the E: drive and have IIS recast everything for you. The downside to this approach is security.
vbnov
January 9th, 2008, 04:06 PM
wow,
Is there any other simple way to access the files from the E:... like giving permissions or something like that?
Because I have the file path stored in the DB and I am assigning the path to a hyperlink.
Thanks!
Yeorwned
January 9th, 2008, 04:10 PM
Move your web application to the E: drive in the same root directory. Even then, you're going to have to use a regular expression to take off the initial E: drive location because browsers will attempt to load that locally because it defaults to file:\\ instead of http:\\ as it is an environmental location.
vbnov
January 9th, 2008, 04:13 PM
Thank you again!
If I have the http:/ in front of the file path and have my .net application on the C: and the files on the E: .... would I be able to access the file?
Yeorwned
January 9th, 2008, 04:21 PM
You are attempting to use a psychical file location on a disk drive. When cast to a remote client's browser, locations generally become relative.
Let's say your application is in C:\Inetpub\wwwoot\. If you display <a href="http://xy%20-z/2008/asd%20def.pdf" /> to the client, the browser will essentially end up requesting the file asd%20def.pdf from the directory "C:\Inetpub\wwwoot\xy%20-z/2008/" which doesn't exist because your file is actually sitting in "E:\xy%20-z\2008\asd%20def.pdf"
Does that make sense? That's just the way browsers and web servers end up rendering files due to client compatibility, security, and server flexibility.
vbnov
January 10th, 2008, 09:20 AM
Thanks a lot for the detailed explanation.
I have a grid view on my webform which has a hyperlink field. Now my question is... Will I be able to implement the above given code in the hyperlink column of the grid view and open the pdf in a separate window?
I am going to try it today. But if you have some idea on this please let me know.
I am using ASP.NET 2.0 & VB.NET.
Help is greatly appreciated. Thanks a bunch!
Yeorwned
January 10th, 2008, 11:10 AM
Yes. The easiest thing for you to do would be to create the download page, which I labeled as POSTBACKPAGE above, and then modify the database to reflect the new address. If you change the database entries, you can leave your gridview untouched.
Changes to database would be fairly simple. If you only have a few, you can do updates by hand. Assuming you have many entries, you can dump the table in question to a .sql file. Then using your favorite text editor, perform a replace all where "E:\xy%20-z\2008" to be "http://YOURWEBSITE.com/POSTBACKPAGE.aspx?selectedfile=" and source the .sql file.
vbnov
January 10th, 2008, 02:17 PM
Thnx.
I created a web form dfile.aspx and added the below code to see if the file is being opened. But instead of opening the .pdf file I get a message if I want to open or save the dfile.aspx. Any ideas why?
Dim FName As String
FName = "E:\xyz\2008\Premium.pdf"
Dim getfile As String = System.IO.Path.GetFileName(FName)
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; getfile=""" & getfile & """")
Response.Flush()
Response.WriteFile(FName)
Could you also explain the below code. Or could you provide the absolute path (ie without the "append" command)
objStringBuilder.Append("<a href=POSTBACKPAGE.aspx?selectedfile=")
objStringBuilder.Append(url).Append(Server.UrlEncode(filepath))
objStringBuilder.Append(">").Append(filepath).Append("</a><br />")
This is my code in case you want to take a look.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" Width="680px">
<Columns>
<asp:Boundfield DataField="NAME" SortExpression = "NAME" HeaderText="Name">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="#74a0ce"></HeaderStyle>
</asp:Boundfield>
<asp:TemplateField HeaderText = "Documents" HeaderStyle-HorizontalAlign = "Center" >
<ItemTemplate>
<a href="javascript:void" onclick = "window.open('<%# EVAL("FILEPATH")%>',null,'height = 500, width = 500,status = no, resizable = yes, scrollbars = yes, toolbar = no,location = no,menubar = no');" >View</a><br />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle BackColor="#74a0ce" ForeColor="White" />
</asp:TemplateField>
</Columns>
</asp:GridView>
FILEPATH = "E:\xyz\2008\premium.pdf" etc.
I would like to open the .pdf in a separate window.
Thanx
Yeorwned
January 10th, 2008, 03:30 PM
Where does your hyperlink point at now? Check this the same way you did before by hovering your mouse over the hyperlink that makes the dfile.aspx dialog popup.
To make it open in a new window, you need to insert a target="_blank" in your ItemTemplate's hyperlink.
Could you also explain the below code. Or could you provide the absolute path (ie without the "append" command)
objStringBuilder.Append("<a href=POSTBACKPAGE.aspx?selectedfile=")
objStringBuilder.Append(url).Append(Server.UrlEncode(filepath))
objStringBuilder.Append(">").Append(filepath).Append("</a><br />")
Since you are using GridView, you do not need this. The first block of code that I posted previously would be used in place of a GridView if you did not already have one and is not required for this to work. It simply created hyperlinks with the file location as the target.
vbnov
January 10th, 2008, 03:45 PM
Here is my hyperlink template column.
<asp:TemplateField HeaderText = "Documents" HeaderStyle-HorizontalAlign = "Center" >
<ItemTemplate>
<a href="javascript:void" onclick = "window.open('<%# EVAL("FILEPATH")%>',null,'height = 500, width = 500,status = no, resizable = yes, scrollbars = yes, toolbar = no,location = no,menubar = no');" >View</a><br />
</ItemTemplate>
on the Grid rowbound the hyperlink will be loaded with the file path.
I didnt quite get your point where you have asked me to replace the path "E:\xyz\2008".
The path currently in the DB is like this - E:\xyz\2008\premium.pdf
Now, Should I replace E:\xyz\2008 with "http://YOURWEBSITE.com/POSTBACKPAGE.aspx?selectedfile="
So the end result will be " http://YOURWEBSITE.com/POSTBACKPAGE.aspx?selectedfile=premium.pdf "
And change the hyperlinks path to dfile.aspx?
Thanks so much for helping me out here.
Yeorwned
January 10th, 2008, 04:11 PM
I didnt quite get your point where you have asked me to replace the path "E:\xyz\2008".
The path currently in the DB is like this - E:\xyz\2008\premium.pdf
Now, Should I replace E:\xyz\2008 with "http://YOURWEBSITE.com/POSTBACKPAGE.aspx?selectedfile="
So the end result will be " http://yourwebsite.com/POSTBACKPAGE.aspx?selectedfile=premium.pdf "
And change the hyperlinks path to dfile.aspx?
I would recommend changing it in the database to reflect the ending hyperlink. Thus, all you have to do is change the path in the database from E:\xyz\2008 with "http://YOURWEBSITE/dfile.aspx?selectedfile=" and you should be done. Remember to substitute your own address and place the directory title in there too if it is not in your root directory.
You can also change it in the ItemTemplate INSTEAD if you like but by changing it in the database, you could put links to other sites/documents in the db and they should show up on the page as well.
vbnov
January 14th, 2008, 11:44 AM
I changed the path in the DB. Now the path is in the following format.
http://www.mysite.com/Download_file.aspx?selectedfile=abc-V4.1.pdf
And I have the followiing code on page_load on the Download_file.aspx
Dim FName As String
FName = Request.QueryString("selectedfile")
Dim getfile As String = System.IO.Path.GetFileName(FName)
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; getfile=""" & getfile & """")
Response.Flush()
Response.WriteFile(FName)
FYI - The files are stored on a different drive E: on the web server. All my .Net field reside under the wwwroot on C:
What am I missing?
I received the following error.
Server Error in '/' Application.
--------------------------------------------------------------------------------
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Download_file.aspx
Yeorwned
January 16th, 2008, 10:14 AM
Did you give IIS permissions to the folder on E:? Assuming your using anonymous access, you need to give IUSR read permissions.
vbnov
January 16th, 2008, 01:56 PM
got it to work. Thanx so much for your help.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.