Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Getting above error while downloading file in my web application.

What I have tried:

C#
string filepathforapp = "http://test.b4live.com" + link.Text.ToString();

string filePath = filepathforapp;
                   
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
                   
Response.WriteFile(filePath);
Response.End();
Posted
Updated 16-Mar-16 3:01am
v2
Comments
CHill60 16-Mar-16 7:28am    
shouldn't there be a space after .com?
[no name] 16-Mar-16 7:31am    
To be able to download the file, the file location need to be available on the web site. Even if the file is not available in your current app, it will have to be available on a different app. Otherwise you can not download it.

If you have an issue or constraint that you can not put it in the app path and your app can access the file (read), you may read the file in your application and rewrite it on the response.

Start by using the debugger (or by logging) to find out exactly what is in filePath when you pass it to Path.GetFileName, and exactly what the method returns as a result.
We can't do that for you - we have no idea what link is, much less what it contains in it's Text property.
At the same time, find out exactly which line of that code is throwing the exception. It should be fairly obvious to you once you have all that information.

BTW: Text is normally a string property, so calling ToString on it just makes your code look like you don;t know what you are doing! :laugh:
 
Share this answer
 
The path you pass to the WriteFile method needs to be either the absolute physical path of the file on your server (eg: C:\Path\To\Your\File.txt), or the virtual path of the file on your site (eg: ~/path/to/your/file.txt, or /path/to/your/file.txt, or ../path/to/your/file.txt).

You are passing in a URL - http ://test.b4live.com/path/to/your/file.txt - which is not supported.

Try something like this:
C#
string filePath = Request.MapPath(link.Text, Request.ApplicationPath, false);
// TODO: Validate that the file path is within the downloads folder.
 
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
                   
Response.WriteFile(filePath);
Response.End();

Pay attention to the comment: You need to make sure that the file you're sending is one you want the user to be able to download. Otherwise, you could end up sending the contents of your web.config file, or the source code for your site.
 
Share this answer
 
v2
Hi
I think the link must be separated by "/" in website virtual path and should be separated by "\" in windows physical folder location so you can try this. I think this will help:
string filepathforapp = "http://test.b4live.com/" + link.Text.ToString();
instead of
string filepathforapp = "http://test.b4live.com" + link.Text.ToString();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900