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?
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?
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
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();
}
thereKen 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
wasa
way to change this default filename, it would be great.
John Smith Jr. said:That worked great, thank you much Ken.
Great help.
FilenameKen 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.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();
}
Ken Cox said:Glad it worked for you!
John Smith Jr. said:That worked great, thank you much Ken.
Great help.
FilenameKen 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.System.EventArgsis
just the friendly/default name to show in the browser.
private void downloadFileButton_Click (object sender,
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();
}