Jump to content
Tuts 4 You

C# and ASP .NET download file question


Kruse

Recommended Posts

* I realized I posted this in the wrong section of the forums, so I moved/reposted it here...Sorry!*

Hi all,

I'm redesigning a website for a non-profit and they have several forms they'd like to be available for download. I'm writing this in C# and asp .NET and have all the forms up there and listed on the page and a script to download them, that I know works. What I would like is when they click on the link for the pdf, they remain on the forms page but a download dialog box appears. my little script makes the dialog (i tested it outside the function). Here's what I have for code:

protected string[] show_forms()
{
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("../Forms"));
DirectoryInfo[] folders = dirInfo.GetDirectories();
string show_forms_str = "empty";
int size = folders.Length;
string[] show_arr = new string[size];
int count = 0;
foreach (DirectoryInfo folder in folders)
{
show_forms_str = "<style type=\"text/css\"> th{font-size:150%; text-align:left}</style><table><tr><th><b>" + folder.Name + "</b></th></tr>";
DirectoryInfo folderInfo = new DirectoryInfo(Server.MapPath("../Forms/" + folder.Name));
FileInfo[] files = folderInfo.GetFiles("*.pdf");
foreach (FileInfo file in files)
{
show_forms_str += "<tr><td><a href= \"#\" onClick=\"dl_file(" + file + ")\">" + file.Name + "</a></tr></td>"; }
show_forms_str += "</table><br>";
show_arr[count] = show_forms_str;
count++;
}
return show_arr;
}
protected void dl_file(FileInfo file)
{
if (file.Exists)
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
Response.End(); }
}

as you can see this will iterate through the directory, get the folder names and subsequent files into a table. for each file, I want to be able to click on it then download the file (onClick="dl_file(file)") all this does right now is send the page back to the top... Any suggestions? If you need more info, I'd be happy to give it to you.

Thanks in advance,

Justin

Link to comment

That's right, you can't call C# function directly from JavaScript.

Few tips:

- Make another page used for sending files (download.aspx maybe, with parameters)

- download.aspx should take parameter like file path, or file ID.

- Make your onclick event redirect to there (new window wont open, and you wont be redirected when that happens, just download window will pop up)

Security tips:

- Encode your file names, don't just print it out to HTML ( HttpUtility.HtmlEncode() )

- If you're getting file path in parameter for download aspx, make sure that it's in right directory, or hacker could download your personal files (download.aspx?filepath=../../../../supersecretdata.txt)

Link to comment
That's right, you can't call C# function directly from JavaScript.

Correct me if I'm wrong/if this is possible, but what I'm trying to do is call a C# function in the page.aspx.cs file in HTML, I'm not using javascript anywhere? Unless the default script for asp.NET is javascript? I'm still new and getting to know asp .NET. Thanks for the responses.

I realize that maybe a bit more info could help. the code posted above is in an aspx.cs file, and returns a string of HTML code, as you seen in that HTML i use the onClick to call the function 'dl_file.' I then call the script in the .aspx page as follows:

<asp:Content ID="Content2" ContentPlaceHolderID="bodyCPH" Runat="Server">	   <%string[] table = show_forms();
foreach (string row in table)
{
Response.Write(row);
} %></asp:Content>

it seems like the HTML string works fine because all the folders and their files are printed out properly, however when I click a link for one of the documents, nothing happens... In short, I don't think I'm using javascript anywhere, just c#, asp and HTML...unless I'm misunderstanding something? Thanks again!

Edited by Kruse
Link to comment
onclick/onanything is JavaScript. On the webpage, it doesn't run your C#. Your C# is only ran server-side.

ah got ya. Thanks. So, if when they click on the link for the form they want to download, is there a way to have asp run that script/notice the link was clicked?

Link to comment
I don't know ASP, but I would suggest following cektop's advice.

I finally had sometime to sit down and get this working, just want to say thanks again to the both of you.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...