Download from button click

  • Thread starter Thread starter John Smith Jr.
  • Start date Start date
J

John Smith Jr.

Is there a way to have the server send a file to client (ie download) from a
button click and a filename string?
 
John Smith Jr. said:
Is there a way to have the server send a file to client (ie download) from a
button click and a filename string?

Yes. And it can even be done with client side code if you want, using an
<input type="button"
onclick="javascript:window.location.href='MyDownload.pdf' /> type of form
element.

if you want to stream it on the server, you can open a FileStream and post
the resulting binary data to the Response.OutputStream

R.
 
Hi John,

If you want a server-side method, try this?

Private Sub LinkButton1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles LinkButton1.Click
Dim FilePath As String = "C:\downloads\setup.exe"
Dim FileName As String = "setup.exe"
'Set the appropriate ContentType.
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=" & FileName)
'Write the file directly to the HTTP output stream.
Response.WriteFile(FilePath)
Response.End()
End Sub


<form id="Form1" method="post" runat="server">
<asp:LinkButton id="LinkButton1" runat="server">Click to
download</asp:LinkButton>
</form>
 
That worked great!
Thank you much.

Had to convert it to C#, but thats pretty easy.

A few questions though, there is a pause before it starts downloading, which
isn't to long but it is local lan, not a big deal though. But it seems to
use "default" as a file name, is there anyway to set what the destination
file name should be by default?
 
Hi John,

It should be using whatever name you supply the FileName variable, which in
my code is "setup.exe". Not sure how Default enters it, unless it is picking
that up from the name of your .aspx page.

As for the speed, it should be quite fast unless something is taking time to
compile or check permissions.

Ken
 
I tried changing the name of the form to default2, and the default file name
changed to default2.

Also, when it shows save as dialog, the file type is blank, i assume this is
because it saves it as "default2" without an extension. But if there was a
way to change this default filename, it would be great.
 
Perhaps you could show us the code so we can demonstrate how it should work?
 
This is the method i am using and it uses the name of the form for the
default filename:


private void downloadFileButton_Click (object sender, System.EventArgs e)
{
Button myButton;
myButton = (Button)sender;

// pass through the filename from the table row
// TODO: convert to viewstate for security
string filename = myButton.Attributes["file"].ToString();

// set http headers and content type
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "Attachment; filename=" +
filename);

// send file
Response.WriteFile(filename);

// disconnect
Response.End();
}
 
Hi John,

Your problem was here near the end:

Response.WriteFile(filepath);

That's where you supply the physical file that you are sending. Filename is
just the friendly/default name to show in the browser.

private void downloadFileButton_Click (object sender, System.EventArgs e)
{
Button myButton;
myButton = (Button)sender;

// pass through the filename from the table row
// TODO: convert to viewstate for security
//string filename = myButton.Attributes["file"].ToString();
string filename = myButton.CommandArgument;
string filepath = "c:\\temp\\setup.exe";
// set http headers and content type
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" +
filename.ToString());
// TODO: John: Remember that the filepath and filename are two
// different issues. One is for your program to find the file
// the other is to give to give a friendly default name for the download
// send file
// Change this to the physical file name you want to send
Response.WriteFile(filepath);

// disconnect
Response.End();
}


John Smith Jr. said:
This is the method i am using and it uses the name of the form for the
default filename:


private void downloadFileButton_Click (object sender, System.EventArgs e)
{
Button myButton;
myButton = (Button)sender;

// pass through the filename from the table row
// TODO: convert to viewstate for security
string filename = myButton.Attributes["file"].ToString();

// set http headers and content type
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "Attachment; filename=" +
filename);

// send file
Response.WriteFile(filename);

// disconnect
Response.End();
}


Ken Cox said:
Perhaps you could show us the code so we can demonstrate how it should work?

file
name this was
 
That worked great, thank you much Ken.

Great help.


Ken Cox said:
Hi John,

Your problem was here near the end:

Response.WriteFile(filepath);

That's where you supply the physical file that you are sending. Filename is
just the friendly/default name to show in the browser.

private void downloadFileButton_Click (object sender, System.EventArgs e)
{
Button myButton;
myButton = (Button)sender;

// pass through the filename from the table row
// TODO: convert to viewstate for security
//string filename = myButton.Attributes["file"].ToString();
string filename = myButton.CommandArgument;
string filepath = "c:\\temp\\setup.exe";
// set http headers and content type
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" +
filename.ToString());
// TODO: John: Remember that the filepath and filename are two
// different issues. One is for your program to find the file
// the other is to give to give a friendly default name for the download
// send file
// Change this to the physical file name you want to send
Response.WriteFile(filepath);

// disconnect
Response.End();
}


John Smith Jr. said:
This is the method i am using and it uses the name of the form for the
default filename:


private void downloadFileButton_Click (object sender, System.EventArgs e)
{
Button myButton;
myButton = (Button)sender;

// pass through the filename from the table row
// TODO: convert to viewstate for security
string filename = myButton.Attributes["file"].ToString();

// set http headers and content type
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "Attachment; filename=" +
filename);

// send file
Response.WriteFile(filename);

// disconnect
Response.End();
}


Ken Cox said:
Perhaps you could show us the code so we can demonstrate how it should work?

I tried changing the name of the form to default2, and the default file
name
changed to default2.

Also, when it shows save as dialog, the file type is blank, i assume this
is
because it saves it as "default2" without an extension. But if
there
was
a
way to change this default filename, it would be great.
 
Glad it worked for you!

John Smith Jr. said:
That worked great, thank you much Ken.

Great help.


Ken Cox said:
Hi John,

Your problem was here near the end:

Response.WriteFile(filepath);

That's where you supply the physical file that you are sending.
Filename
is
just the friendly/default name to show in the browser.

private void downloadFileButton_Click (object sender, System.EventArgs e)
{
Button myButton;
myButton = (Button)sender;

// pass through the filename from the table row
// TODO: convert to viewstate for security
//string filename = myButton.Attributes["file"].ToString();
string filename = myButton.CommandArgument;
string filepath = "c:\\temp\\setup.exe";
// set http headers and content type
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" +
filename.ToString());
// TODO: John: Remember that the filepath and filename are two
// different issues. One is for your program to find the file
// the other is to give to give a friendly default name for the download
// send file
// Change this to the physical file name you want to send
Response.WriteFile(filepath);

// disconnect
Response.End();
}
 
Hi Ken/John,

I have seen this code several times, but I would like to download all the
files from a folder (of various types i.e. .txt, .dll, .dat, etc.). I tried

Dim MyDir As IO.Directory
Dim path As String = Me.MapPath(".") & "\Data"
Dim dirs As String() = IO.Directory.GetFiles(path)
Dim iLoop As Integer
Dim FileName As String
'Set the appropriate ContentType.
Response.ContentType = "multipart/mixed"

For iLoop = dirs.GetLowerBound(0) To dirs.GetUpperBound(0) - 1
FileName = dirs(iLoop).Substring(dirs(iLoop).LastIndexOf("\") + 1)
Response.AppendHeader("Content-Disposition", "attachment; filename=" &
FileName)
Response.WriteFile(dirs(iLoop))
Next

This gets a single file with the name of the first file with all the files
combined. I have tried the multipart/mixed, but I not sure what to use as a
boundry or how to get the multiple filenames.

Any advice would be appreciated,

Thanks,

Stephen
Ken Cox said:
Glad it worked for you!

John Smith Jr. said:
That worked great, thank you much Ken.

Great help.


Ken Cox said:
Hi John,

Your problem was here near the end:

Response.WriteFile(filepath);

That's where you supply the physical file that you are sending.
Filename
is
just the friendly/default name to show in the browser.

private void downloadFileButton_Click (object sender,
System.EventArgs
e)
{
Button myButton;
myButton = (Button)sender;

// pass through the filename from the table row
// TODO: convert to viewstate for security
//string filename = myButton.Attributes["file"].ToString();
string filename = myButton.CommandArgument;
string filepath = "c:\\temp\\setup.exe";
// set http headers and content type
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" +
filename.ToString());
// TODO: John: Remember that the filepath and filename are two
// different issues. One is for your program to find the file
// the other is to give to give a friendly default name for the download
// send file
// Change this to the physical file name you want to send
Response.WriteFile(filepath);

// disconnect
Response.End();
}
 
Back
Top