File download using C#

You would have tried downloading a file from the Internet. And, one of the following would have happened: 1. the "Save As" dialog box will show from where you can choose to save the file, or 2. the file will be opened in the web browser itself.The latter happens because the application that normally is used to open the file would have been installed on the machine. What if we wanted to show the "Save As" dialog box in any case? Use "Response.AddHeader"If a string con ...

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 124
January 22, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement
You would have tried downloading a file from the Internet. And, one of the following would have happened: 1. the "Save As" dialog box will show from where you can choose to save the file, or 2. the file will be opened in the web browser itself.The latter happens because the application that normally is used to open the file would have been installed on the machine. What if we wanted to show the "Save As" dialog box in any case? Use "Response.AddHeader"If a string contains special characters we should either use the "Escape sequence" or the "Verbatim string". Note that in the example below I have used the "Verbatim String" character (i.e., @) in the line where I open a FileStream. [bold]The complete code listing follows:[/bold]


<%@ Page Language="C#" %>
<%@ 
Import Namespace="System.IO" %>

<
html>
  <
head>
    <
title>C#: File download</title>
    
<script runat="server">
      
void Page_Load(object senderEventArgs e) {
        if (
Page.IsPostBack){
          
          
Response.ContentType="application/ms-word";
          
Response.AddHeader"content-disposition","attachment; filename=download.doc");
                
          
FileStream sourceFile = new FileStream(@"F:downloadexample.doc"FileMode.Open);
          
long FileSize;
          
FileSize sourceFile.Length;
          
byte[] getContent = new byte[(int)FileSize];
          
sourceFile.Read(getContent0, (int)sourceFile.Length);
          
sourceFile.Close();

          
Response.BinaryWrite(getContent);
        }
      }
    
</script>
  </head>
  
  <body>
    <form runat="server" id="frmDownload">
      <asp:button id="btnDownload" Text = "Click here to download" runat="server" />
    </form>
  </body>
</html>

blog comments powered by Disqus
CODE EXAMPLES ARTICLES

- Bipartite Graphs
- Connectivity in Graphs
- The Ford-Fulkerson Algorithm
- Critical Paths
- The Bellman-Ford and Roy-Floyd Algorithms
- Shortest Path Algorithms in Graphs
- Minimum Spanning Tree
- Articulation Edges and Vertexes
- Circles and Connectivity in Graphs
- Depth-First Search in Graphs
- Breadth-First Search in Graphs
- The Prufer Code and the Floyd-Warshall Algor...
- An Insight into Graphs
- Coding a Custom Object with WSC
- Creating a Custom Object with WSC

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials