Downloading File

  • Thread starter Thread starter Steven
  • Start date Start date
S

Steven

Hello

In my asp.net application,I will be showing some PDFs which the user can
download. When the user clicks on any PDF I will be downloading the related
file and I will be showing the related portion of the map in the image box.
When the user clicks on any file, the ImageBox is getting the correct URL
but its not showing. If I disable the Downloadfile function, the image box
is getting and showing the correct URL. How can I solve this problem

here is my download file function:

public void DownloadFileToClientPC()
{
try
{
string FullPathOfTheFile = "C:\\TestFile.tif" ;
System.IO.FileInfo FileToDownLoad = new
System.IO.FileInfo(FullPathOfTheFile);
this.Page.Response.Clear() ;
this.Page.Response.ContentType = "application/octet-stream" ;
this.Page.Response.AddHeader("content-disposition",
"attachment; filename="+ FileToDownLoad.Name);
this.Page.Response.WriteFile(FileToDownLoad.FullName) ;
this.Page.Response.Flush() ;
this.Page.Response.Close() ;
}
catch(Exception ex)
{

}
}


Thanks
 
Steven said:
Hello

In my asp.net application,I will be showing some PDFs which the user can
download. When the user clicks on any PDF I will be downloading the related
file and I will be showing the related portion of the map in the image box.
When the user clicks on any file, the ImageBox is getting the correct URL
but its not showing. If I disable the Downloadfile function, the image box
is getting and showing the correct URL. How can I solve this problem

You can't do what you are trying to do the way you are trying to do it.
The download code you've got is sending the file instead of the content
of the page. If you use that code, you must run it in a separate page.
You might be able to get the effect you want using frames / iframes.
 
Thanks Steve for the reply. Could you please tell me how to download the
file in a seperate window.

Thanks
Steven
 
I have one more question. If we have a hyperlink we just have to specify the
NavigateURL to C:\TestFile.tif and it will automatically download it. Why do
we have to write the download procedure when we are using a
button/linkbutton/imagebutton. Can't we do something similar as in
Hyperlink?
 
See below.
I have one more question. If we have a hyperlink we just have to specify the
NavigateURL to C:\TestFile.tif and it will automatically download it. Why do
we have to write the download procedure when we are using a
button/linkbutton/imagebutton. Can't we do something similar as in
Hyperlink?

The difference is that a hyperlink in html is an instruction to the
user's web browser to go to the specified url and download whatever is
there. An ASP.NET button/linkbutton/imagebutton effectively causes the
browser to request the same page again passing enough information for
the server to work out which button/linkbutton/etc has been clicked. The
server then runs your C# code associated with that control.

Using the download code you have, you are basically sending the contents
of the file back instead of your webpage. This can be a useful
technique, particularly when you don't want people to be able to link
directly to your content; maybe you want to check that they are logged
in, or record their visit in a database, or whatever. If they can just
download the file without visiting any of your pages, you can't do it.
It's also useful if the file does not actually exist; maybe the content
of the file is stored in a database, or maybe you are creating it on the
fly.

Looks to me like your problem is that you want to run some code on the
server (because you want to insert a thumbnail of the currently
downloading file) but you also want to tell the browser to open a new
window to download the file. There are a few ways round this. Easiest
way if you can live with it is to show the preview and then provide the
download link, so when they click on your linkbutton you display the
thumbnail and a simple hyperlink to download the content. Make the
hyperlink like:

<a href="http://www.foo.bar/stuff.pdf" target="_blank">Stuff</a>

target="_blank" will cause a new window to open. Some users don't like
this, by the way.

The downside of this is that you will need two clicks. Alternatives are
to do it all with client-side scripting; either you insert some
javascript to start the download in a new window which is fired from the
same client-side event as the one that causes your linkbutton to
postback, or else in your server-side C# code running in the click event
of the linkbutton you set the thumbnail image to display and then insert
some javascript into the page to open the download. Downside of this is
that some browsers won't allow new windows to be opened via script
unless the user allows it.

To get you started, here is how you could insert some javascript into
the page when the linkbutton is clicked:

private void LinkButton1_Click(object sender, System.EventArgs e)
{
this.Page.RegisterClientScriptBlock("download","<script
language=javascript>window.open(\"http://www.google.com\");</script>");
}
 

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