Solved

Open file using FileInfo from a server...

Posted on 2007-11-13
10
1,690 Views
Last Modified: 2012-05-05
Hi,
I'm trying to downLoad a file from a server, but get an error : URI formats are not supported
This is the code :
    public bool DownLoadFile()
    {
        string path = ConfigurationManager.AppSettings["FilesPathURL"].ToString();
        string filepath = path + "log".txt";
        FileInfo file = new FileInfo(filepath);
       
        if (file.Exists)
        {
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
            HttpContext.Current.Response.ContentType = "application/ms-word";
            HttpContext.Current.Response.WriteFile(file.FullName);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
            return true;
        }
        else
        {
            return false;
        }
    }

In the Web.config :
<add key="FilesPathURL" value="http://test.co/DownLoadedFiles/" />


What is the problem ?
Thanks
0
Comment
Question by:udir
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:cezarF
Comment Utility
try converting the filepath to physical file path

FileInfo file = new FileInfo(Server.MapPath(filepath));

0
 
LVL 9

Expert Comment

by:ripahoratiu
Comment Utility
The URL. This problem was asked before. An URL is an alias, you cannot reference a file via an URL. On the server side you have a normal windows path to your file, not an URL.
Therefore:
string path = ConfigurationManager.AppSettings["FilesPathURL"].ToString();
string filepath = path + "log".txt";
FileInfo file = new FileInfo(filepath);
with
<add key="FilesPathURL" value="http://test.co/DownLoadedFiles/" />
is not valid
0
 
LVL 9

Expert Comment

by:ripahoratiu
Comment Utility
Be aware that you may encounter security issues, due the fact that your web server credentials may not grant access to all physical paths. i.e. on IIS, by default, you only can access the virtual directory where the app is and it's subdirectories.
0
 
LVL 1

Author Comment

by:udir
Comment Utility
Hi,
I tryied :
FileInfo file = new FileInfo(Server.MapPath(filepath));
but get an error :
http://test.co/DownLoadedFiles/log.txt  is not a valid virtual path

Any idea?

0
 
LVL 1

Author Comment

by:udir
Comment Utility
And if i put that address in my browser, the file is being opened in the browser.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 9

Expert Comment

by:ripahoratiu
Comment Utility
A virtual path is also an alias. Use HttpRequest.ApplicationPath to find out the root of your virtual path, if the file is relative to that. Orelse use something like "D:\Files\Downloadable\"
0
 
LVL 1

Author Comment

by:udir
Comment Utility
Can u give an example of how to use HttpRequest.ApplicationPath in my function ?
0
 
LVL 9

Expert Comment

by:ripahoratiu
Comment Utility
Response.WriteFile(Request.ApplicationPath + "log".txt"; );
Not sure if Request.ApplicationPath ends with "\" or you should put it in "\log.txt"
This assumes that the log.txt is in the directory of the application (if you're using IIS is the Local Path in web site/virtual directory directory properties).
0
 
LVL 14

Accepted Solution

by:
cezarF earned 500 total points
Comment Utility
2 options

1. open IIS and create a virtual folder for your app or...
2. open IIS display app property and create "Application Name" under directory

then change

   <add key="FilesPathURL" value="http://test.co/DownLoadedFiles/" />

to

<add key="FilesPathURL" value="/VirtualFolder" />

and

string path = ConfigurationManager.AppSettings["FilesPathURL"].ToString();
string filepath = path + "/DownLoadedFiles/log".txt";
FileInfo file = new FileInfo(Server.MapPath(filepath));

0
 
LVL 4

Expert Comment

by:kjetilroe
Comment Utility
Hi,

I think what you are trying to do is impossible. Here is a solution that I have used before:

1. If the file exist on the web-page, download it using this module:
http://www.codeproject.com/cs/internet/webdownload.asp

2. After you have downloaded the file to a local directory, you can share it out to your users. The way you have done in your web-page.

Another way is that you can use your web-page as a proxy, and use the module that I have provided you as a guideline to forward the files from the remote web-page to the users of your web-page.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now