how can I put in a procedure to download a file

I have been searching and searching - but it is hard to find anything useful when the key word is "download"

Anyway here is what I would like to know how to do

If I have a WebSite created in VS2005 and I add a button to Default.aspx, I would like to have a file to  be downloaded on the user's computer in a directory that they choose. I would think that this should not be too hard but I just can't seem to find out how to do this no matter how hard I search.

thanks

 



Answer this question

how can I put in a procedure to download a file

  • Pairair

    thanks for your reply  -  I will look at this further  -  my first attempts at using this are not very successful.  My intention would be to be able to have the user select a directory where the file will be downloaded into and have a window that comes up when a file is  downloaded like the window that normally comes up asking for the directory and then showing the progress and finally the download Complete indication - just as every other download works

     

    thanks


  • Greg1

    I think the problem may exist in having the file ( a simple test.zip ) located in the App_Data folder - When I created another folder in the project call "downloads" and moved the file there I did not get the 403 error. Things worked like I wanted them to -  so I guess I can move on  - I assume that the App_Data folder has special properties that prevents me from doing this.

    thanks for all your help

     

     


  • Oya ORER

    He is also right. The function I provided you with lets the user download a file from a specified location, TO a specified location.

  • DentonChris

    Place your files in a download folder, and give your internet users access to the folder. You can post links to files in this folder, and if a user clicks on one of these links, IE wil automatically prompt the user with the download dialog.

  • Lesley-Ann Vaughan

    I usually dont post any replys because I am not an expert, but I think I could help you on this. The following code will be the download function. You may just copy and past it. Remember to add the namespaces. I dont know if this is what you are looking for, but please reply and let me know if it helped.

    using System.IO; & using System.Net;

    public static int DownloadFile(String remoteFilename,
                                   String localFilename)
    {
      // Function will return the number of bytes processed // to the caller. Initialize to 0 here.
      int bytesProcessed = 0;

      // Assign values to these objects here so that they can
      // be referenced in the finally block

      Stream remoteStream  = null;
      Stream localStream   = null;
      WebResponse response = null;

      // Use a try/catch/finally block as both the WebRequest and Stream
      // classes throw exceptions upon error

      try
      {
        // Create a request for the specified remote file name
        WebRequest request = WebRequest.Create(remoteFilename);
        if (request != null)
        {
          // Send the request to the server and retrieve the
          // WebResponse object

          response = request.GetResponse();
          if (response != null)
          {
            // Once the WebResponse object has been retrieved,
            // get the stream object associated with the response's data

            remoteStream = response.GetResponseStream();

            // Create the local file
            localStream = File.Create(localFilename);

            // Allocate a 1k buffer
            byte[] buffer = new byte[1024];
            int bytesRead;

            // Simple do/while loop to read from stream until
            // no bytes are returned

            do
            {
              // Read data (up to 1k) from the stream
              bytesRead = remoteStream.Read (buffer, 0, buffer.Length);

              // Write the data to the local file
              localStream.Write (buffer, 0, bytesRead);

              // Increment total bytes processed
              bytesProcessed += bytesRead;
            } while (bytesRead > 0);
          }
        }
      }
      catch(Exception e)
      {
        Console.WriteLine(e.Message);
      }
      finally
      {
        // Close the response and streams objects here
        // to make sure they're closed even if an exception
        // is thrown at some point

        if (response     != null) response.Close();
        if (remoteStream != null) remoteStream.Close();
        if (localStream  != null) localStream.Close();
      }

      // Return total bytes processed to caller.
      return bytesProcessed;
    }

    Finally, here's an example of using the DownloadFile function.

    int read = DownloadFile("http://www.mysite.com/problem1.jpg",
                "d:\\test.jpg");
    Console.WriteLine("{0} bytes written", read);
    


  • blaineca

    When I try this I am getting the error: HTTP Error 403 - Forbidden.

    In the properties for the folder where this file exists ( App_Data ), on the Shering Tab, I set it to "Share this Folder" but no luck - still get the error

    I tried adding ASP.NET ( machine account ) on the Security Tab to have Read and Execute but still no luck  -  right now I am just trying this in VS2005 locally and cannot get it to work  -   How do I give access to this file

    thanks


  • how can I put in a procedure to download a file