File download help

  • Thread starter Thread starter Millhouse50
  • Start date Start date
M

Millhouse50

I am wondering how I can create like a dynamic hyperlink using C#. I am
working with asp.net 2.0 and iis5 on windows 2000 server. I am creating
a file in a program that I have which needs to be downloaded at the end
of the program. Like when the program is done running the user right
clicks on the link and then checks save as and then it's on there
computer. Only thin is that the file name to be downloaded is going to
change with each run of the program. I was wondering if anyone knew
where some code was, or could give me a little sample of how to work
this. Thanks a ton for help.
 
Hi Millhouse,

I think you may need to explain in more detail.

Do you mean you have a web application with a link to a file, and the file
is created when the web application runs some program?
You can always set the link during postback, or use
Response.WriteFile/TransmitFile.
 
Yeah this would be at the end of a web app. I am using the wizard
control from asp.net nad have four pages to the wizard. I am trying to
take the user entered url given in text box on first page, and by the
time i'm done with the app I need to download that file as a .pfx. So
something like www.go.com.pfx, but the URL changes everytime the user
runs the app, so I need to make the link dynamic so when the user
clicks the finish button on the wizard app the .pfx will be created and
there will be a link on a completion page where you can download it.
 
Here is some of the code that I have. I have a bit of a different
question now though. I was wondering if I could put a timeout event
right before it executes the DownloadFile code so that my program can
wait and make sure that the file is created to be downloaded


psi.CreateNoWindow = true;
System.Diagnostics.Process.Start(psi);
certnameLabel.Text = urlTextBox.Text;
certcityLabel.Text = cityTextBox.Text;
certstateLabel.Text = stateTextBox.Text;
certcountryLabel.Text = countryTextBox.Text;
DownloadFile(File, true);





private void DownloadFile(string fname, bool forceDownload)
{
string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".pfx":
type = "application/x-pkcs12";
break;
}
}
if (forceDownload)
{
Response.AppendHeader("content-disposition",
"attachment; filename=" + name);
}
if (type != "")
Response.ContentType = type;
Response.WriteFile(path);
Response.End();
 
I was thinking of monitoring the directory, and for windows applications
you could then be notified when the file is made, but I'm not sure this
will work for web applications. A couple of other solutions might be to
hold page_load until the file is made, maybe polling the directory, or set
the page to update itself in n seconds, making sure to give a large enough
time span to ensure the file is made.

Easiest method is probably to hold the web page until the file is made.
Possibly showing an animated gif while waiting.


Here is some of the code that I have. I have a bit of a different
question now though. I was wondering if I could put a timeout event
right before it executes the DownloadFile code so that my program can
wait and make sure that the file is created to be downloaded


psi.CreateNoWindow = true;
System.Diagnostics.Process.Start(psi);
certnameLabel.Text = urlTextBox.Text;
certcityLabel.Text = cityTextBox.Text;
certstateLabel.Text = stateTextBox.Text;
certcountryLabel.Text = countryTextBox.Text;
DownloadFile(File, true);





private void DownloadFile(string fname, bool forceDownload)
{
string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".pfx":
type = "application/x-pkcs12";
break;
}
}
if (forceDownload)
{
Response.AppendHeader("content-disposition",
"attachment; filename=" + name);
}
if (type != "")
Response.ContentType = type;
Response.WriteFile(path);
Response.End();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top