Solved

Open file using FileInfo from a server...

Posted on 2007-11-13
10
1,741 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 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