Results 1 to 6 of 6

Thread: [2005] Downloading file off another site

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    [2005] Downloading file off another site

    Hello,

    I am currently making few changes in the website for an organization where they have some files on another site, on the same server, and in the page, they have sort of hard-coded the link. I mean, the link looks like:
    Code:
    http://domain/folder/subfolder/file.xls
    .
    It works like a normal link.

    Now I have to change as per these specifications:

    1) check which user is downloading which file.
    2) To publish the website on another server, this time they are willing to keep the files in separate folder (outside the actual website).
    3) Implementing Forms Authentication.
    well the list is too long.

    The approach I am taking is to take user to another page upon clicking of link, authenticate the user there, make a log, and download the file.

    The problem is downloading part. I can't get it working. I tried with Response.WriteFile(), TransmitFile(), WebClient.DownloadFile() (well this works, but this will save the file to some specific location and user won't get the prompt. So I am trying to avoid this). If I specify the physical location of the file, then it works, but absolute URI doesn't.

    How can it be done? I open for other suggestions too.

    Thank you
    Show Appreciation. Rate Posts.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] Downloading file off another site

    Hey,

    Normally when I have had to do this, I have the contents of the file in a byte array, and then I write the contents of the byte array to the Response as well as setting the ContentType. This then prompts the user to select where they want to download the file to.

    For instance:

    Code:
       With Response
          .AppendHeader("Content-disposition", "filename=Report.xls")
          .ContentType = "application/vnd.ms-excel"
          .OutputStream.Write(newFile, 0, newFile.Length)
          .Flush()
          .Close()
        End With
    Where newFile is the byte array.

    Hope that helps!!

    Gary

  3. #3

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: [2005] Downloading file off another site

    Quote Originally Posted by gep13
    Hey,

    Normally when I have had to do this, I have the contents of the file in a byte array, and then I write the contents of the byte array to the Response as well as setting the ContentType. This then prompts the user to select where they want to download the file to.

    For instance:

    Code:
       With Response
          .AppendHeader("Content-disposition", "filename=Report.xls")
          .ContentType = "application/vnd.ms-excel"
          .OutputStream.Write(newFile, 0, newFile.Length)
          .Flush()
          .Close()
        End With
    Where newFile is the byte array.

    Hope that helps!!

    Gary
    I tried that too. Didn't work. Threw some error. I cannot recall it right now.

    I will give it a try again and let you know.
    Show Appreciation. Rate Posts.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] Downloading file off another site

    Hey,

    I have used the above technique as recently as yesterday, and it worked really well.

    If you get the error again, post it and we will see if we can get it to work.

    Gary

  5. #5
    Junior Member
    Join Date
    Feb 2009
    Posts
    26

    Smile Re: [2005] Downloading file off another site

    Hi,

    I read your query and I want to know if what I state below is what you have in mind? As I understand you server hosts a number of web sites and the file to be downloaded to the client is inside one web site hosted by the computer and your page from which you want to write your download code is in some other website hosted by the same computer. That is they are in 2 different domains. Is that it? If that were the case then I am afraid you will have to find the physical path of the directory in which the files are and hard code it.

    I will give below the code for downloading any file available inside the application root of the web site in which the download page page exists.

    I am giving below typical code for a word file called justfordownload.doc inside a directory called "Files" under your application root. Create a dummy word file by that name and put it in the directory called "Files" in your application root. And in the argument for TransmitFile you have to give the full path name of the file to be transmitted. Now create a web application with a page which has a button and in the click event of the button put the code I give below.

    Code:
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + "justfordownload.doc");
            Response.ContentType = "application/ms-word";
            string ms = Server.MapPath("~"); // This will get your application's physical root
            ms +=  @"\Files\justfordownload.doc"; // append the file's relative path to the application's physical root
            Response.TransmitFile(ms);
            Response.Flush();
            Response.End();
    When you run the page and click on the button you will get a prompt for download and you will be able to download the file. The main thing is Response.ContentType should be set to the proper mime type. Since this example is for a word file it is set to "application/ms-word". If it were an excel file you have to download, it would be different.

    To add the appropriate mime type you have to find the extension of the file and from that decide whether it is a word file or an excel file and specify the appropriate mime type.

    The different known mime types can be found at the following url

    http://msdn.microsoft.com/en-us/library/ms775147.aspx

    But if your situation is not as simple as this and you want to download files in one domain from a page in a different domain in the same server then it is a different issue altogether. If all the files are in the same directory in a different domain then you may have to hard code the physical path of that directory for use in TransmitFile.

    Hope this helps.

    Warm Regards,
    sr_jay

    Quote Originally Posted by Harsh Gupta
    Hello,

    I am currently making few changes in the website for an organization where they have some files on another site, on the same server, and in the page, they have sort of hard-coded the link. I mean, the link looks like:
    Code:
    http://domain/folder/subfolder/file.xls
    .
    It works like a normal link.

    Now I have to change as per these specifications:

    1) check which user is downloading which file.
    2) To publish the website on another server, this time they are willing to keep the files in separate folder (outside the actual website).
    3) Implementing Forms Authentication.
    well the list is too long.

    The approach I am taking is to take user to another page upon clicking of link, authenticate the user there, make a log, and download the file.

    The problem is downloading part. I can't get it working. I tried with Response.WriteFile(), TransmitFile(), WebClient.DownloadFile() (well this works, but this will save the file to some specific location and user won't get the prompt. So I am trying to avoid this). If I specify the physical location of the file, then it works, but absolute URI doesn't.

    How can it be done? I open for other suggestions too.

    Thank you

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Downloading file off another site

    Because the file is in another site (or did I get that wrong?), you need to do an HttpWebRequest, get the ResponseStream, read it, get the byte[] array and then write that byte[] array to the client using Response.BinaryWrite. That'll give them a prompt.

Posting Permissions

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



Click Here to Expand Forum to Full Width