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.AppSe ttings["Fi lesPathURL "].ToStrin g();
string filepath = path + "log".txt";
FileInfo file = new FileInfo(filepath);
if (file.Exists)
{
HttpContext.Current.Respon se.ClearCo ntent();
HttpContext.Current.Respon se.AddHead er("Conten t-Disposit ion", "attachment; filename=" + file.Name);
HttpContext.Current.Respon se.AddHead er("Conten t-Length", file.Length.ToString());
HttpContext.Current.Respon se.Content Type = "application/ms-word";
HttpContext.Current.Respon se.WriteFi le(file.Fu llName);
HttpContext.Current.Respon se.Flush() ;
HttpContext.Current.Respon se.End();
return true;
}
else
{
return false;
}
}
In the Web.config :
<add key="FilesPathURL" value="http://test.co/DownLoadedFiles/" />
What is the problem ?
Thanks
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.AppSe
string filepath = path + "log".txt";
FileInfo file = new FileInfo(filepath);
if (file.Exists)
{
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
return true;
}
else
{
return false;
}
}
In the Web.config :
<add key="FilesPathURL" value="http://test.co/DownLoadedFiles/" />
What is the problem ?
Thanks
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.AppSe ttings["Fi lesPathURL "].ToStrin g();
string filepath = path + "log".txt";
FileInfo file = new FileInfo(filepath);
with
<add key="FilesPathURL" value="http://test.co/DownLoadedFiles/" />
is not valid
Therefore:
string path = ConfigurationManager.AppSe
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.
ASKER
Hi,
I tryied :
FileInfo file = new FileInfo(Server.MapPath(fi lepath));
but get an error :
http://test.co/DownLoadedFiles/log.txt is not a valid virtual path
Any idea?
I tryied :
FileInfo file = new FileInfo(Server.MapPath(fi
but get an error :
http://test.co/DownLoadedFiles/log.txt is not a valid virtual path
Any idea?
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.ApplicationPat h to find out the root of your virtual path, if the file is relative to that. Orelse use something like "D:\Files\Downloadable\"
ASKER
Can u give an example of how to use HttpRequest.ApplicationPat h in my function ?
Response.WriteFile(Request .Applicati onPath + "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).
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
FileInfo file = new FileInfo(Server.MapPath(fi