Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open file using FileInfo from a server...

Posted on 2007-11-13
10
Medium Priority
?
1,792 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

886 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