Results 1 to 5 of 5

Thread: File Download code in asp.net.

  1. #1
    HowardAdams is offline Senior Member
    Join Date
    Dec 2009
    Posts
    214
    Rep Power
    3

    Default File Download code in asp.net.

    Hi. I am first year MCA student. I am doing one project. In that project one download section there. For that I want download code. I tried different code but none of them worked out. Please help me as soon as possible. It is highly appreciated. Thanks in advanced.

  2. #2
    HowardAllen is offline Senior Member
    Join Date
    Dec 2009
    Posts
    196
    Rep Power
    3

    Default

    It is very simple. Have you tried below code to file download? If not then try this

    Code:
    Response.ContentType = "test.doc";
    Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.jpg");
    Response.TransmitFile( Server.MapPath("~/download/test.doc") );
    Response.End();

  3. #3
    MyersGray is offline Senior Member
    Join Date
    Dec 2009
    Posts
    197
    Rep Power
    3

    Default

    I suggest you to refer following illustration, which will give you something idea about the use of this:

    Code:
    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 ".htm":
        case ".html":
          type = "text/HTML";
          break;
      
        case ".txt":
          type = "text/plain";
          break;
         
        case ".doc":
        case ".rtf":
          type = "Application/msword";
          break;
        }
      }
      if ( forceDownload )
      {
        Response.AppendHeader( "content-disposition",
            "attachment; filename=" + name );
      }
      if ( type != "" )   
        Response.ContentType = type;
      Response.WriteFile( path );
      Response.End();

  4. #4
    SullivanWalker is offline Senior Member
    Join Date
    Dec 2009
    Posts
    189
    Rep Power
    3

    Default

    Refer the following code which is given below. It is very simple understand.

    Code:
    <%@ Page language="vb" runat="server" explicit="true" strict="true" %>
    	<script language="vb" runat="server">
    	Sub Page_Load(Sender As Object, E As EventArgs)
    	    Dim strRequest As String = Request.QueryString("file") '-- if something was passed to the file querystring
    	    If strRequest <> "" Then 'get absolute path of the file
    	        Dim path As String = Server.MapPath(strRequest) 'get file object as FileInfo
    	        Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) '-- if the file exists on the server
    	        If file.Exists Then 'set appropriate headers
    	            Response.Clear()
    	            Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
    	            Response.AddHeader("Content-Length", file.Length.ToString())
    	            Response.ContentType = "application/octet-stream"
    	            Response.WriteFile(file.FullName)
    	            Response.End 'if file does not exist
    	        Else
    	            Response.Write("This file does not exist.")
    	        End If 'nothing in the URL as HTTP GET
    	    Else
    	        Response.Write("Please provide a file to download.")
    	    End If
    	End Sub
    	</script>

  5. #5
    OrtizCooper is offline Banned
    Join Date
    Dec 2009
    Posts
    186
    Rep Power
    0

    Default

    Follow the given steps:

    1. Make a new ASP.NET project

    2. Add a new form called downloadtest.aspx and paste this part of code in the aspx file page_load procedure.

    Code:
    Private Sub Page_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Response.ContentType = "applicationoctet-stream"
     Dim filename as string=new string("c:\downloads\testdoc.docx") 
            Dim downloadFile As System.IO.FileStream = New System.IO.FileStream(filename, IO.FileMode.Open)
            Response.Write(downloadFile.Length() & "#")
            downloadFile.Close()
            Response.WriteFile(filename)
            Response.Flush()
            Response.End()
    End Sub

Similar Threads

  1. Asp.net code for download file
    By AllenBrown in forum other peripherals
    Replies: 2
    Last Post: 06-29-2010, 03:33 PM
  2. Code for file upload in VB.net
    By BakerJones in forum Programming
    Replies: 2
    Last Post: 06-17-2010, 01:53 PM
  3. File Download code in asp.net.
    By HowardAdams in forum General Internet Terms
    Replies: 4
    Last Post: 04-03-2010, 12:15 PM
  4. Java code to run batch file
    By RodríguezBrown in forum Programming
    Replies: 3
    Last Post: 03-31-2010, 03:53 PM
  5. Can't Download file or videos
    By joshin in forum General Internet Terms
    Replies: 1
    Last Post: 01-10-2009, 07:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48