Solved

Open file using FileInfo from a server...

Posted on 2007-11-13
10
1,714 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
ID: 20278164
try converting the filepath to physical file path

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

0
 
LVL 9

Expert Comment

by:ripahoratiu
ID: 20278209
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
ID: 20278223
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 1

Author Comment

by:udir
ID: 20278402
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
ID: 20278426
And if i put that address in my browser, the file is being opened in the browser.
0
 
LVL 9

Expert Comment

by:ripahoratiu
ID: 20278500
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
ID: 20278537
Can u give an example of how to use HttpRequest.ApplicationPath in my function ?
0
 
LVL 9

Expert Comment

by:ripahoratiu
ID: 20278596
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
ID: 20278646
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
ID: 20278867
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Diagnostics with Net and Net.Sockets 2 30
asp day pilot 3 21
C# LINQ ForEach() question 6 29
Linq asp.net mvc 13 12
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

825 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