Link to home
Start Free TrialLog in
Avatar of udir
udir

asked on

Open file using FileInfo from a server...

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
Avatar of cezarF
cezarF
Flag of Australia image

try converting the filepath to physical file path

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

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
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.
Avatar of udir
udir

ASKER

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?

Avatar of udir

ASKER

And if i put that address in my browser, the file is being opened in the browser.
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\"
Avatar of udir

ASKER

Can u give an example of how to use HttpRequest.ApplicationPath in my function ?
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).
ASKER CERTIFIED SOLUTION
Avatar of cezarF
cezarF
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.