locked
File streaming problem in a live environment RRS feed

  • Question

  • User242099322 posted

    Hi, I’m trying to stream a file to a user’s browser when they click a download link.

    The files are predominately .rar. My code works fine when I run it locally but when I publish the project and run it in live the getfile.aspx page opens in a new browser but the download doesn’t start. I have tried changing the ContentType as I thought it might be something to do with this but nothing seems to work. Does anyone have any ideas??

    protected void Page_Load(object sender, EventArgs e)
            {
                //get document name from query string
                string fileName = Request.QueryString["DocName"];
    
                if (fileName != null)
                {
                    try
                    {
                        List<DownloadFile> _downloadFile = DataLayer.GetDownload(fileName);                    
    
                        //set the virtual path of the file                    
                        string downloadLocation = Server.MapPath("/downloads/" + fileName);
    
                        //set up the file details ready for download
                        FileInfo file = new FileInfo(downloadLocation);
    
                        if (file.Exists)
                        {
                            //clear the current output content from the buffer
                            Response.Clear();
    
                            //add the header that specifies the default filename for the Download/SaveAs dialog
                            Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadLocation);
    
                            //add the header that specifies the file size, so that the browser can show the download progress
                            Response.AddHeader("Content-Length", file.Length.ToString());
    
                            //specify that the response is a stream that cannot be read by the client and must be downloaded
                            Response.ContentType = "application/octet-stream";
    
                            //stream file to user                     
                            Response.WriteFile(file.FullName);
                            Response.End();
                        }
                        else
                        {
                            //file doesnt exist
                        }
                    }
                    catch (Exception ex)
                    {
                        Label1.Text = ex.ToString();
                    }
                }
                else
                {
                    //Response.Redirect("error page");
                }
            }


     

    Wednesday, October 14, 2009 4:32 PM

Answers

  • User2089561920 posted

    hmm this is really wierd, although where exactly in your code is the update to db?

    perhaps you can do a try catch for the whole code, and change the label1.text to response.write and see if anything shows

    else take out the filedownload portion and change it to a response.write, then slowly add in code section by section and see which code screws up

    1. protected void Page_Load(object sender, EventArgs e)  
    2.         {  
    3.             //get document name from query string  
    4.             string fileName = Request.QueryString["DocName"];  
    5.   
    6.             if (fileName != null)  
    7.             {  
    8.                 try  
    9.                 {  
    10.                     List<DownloadFile> _downloadFile = DataLayer.GetDownload(fileName);                      
    11.   
    12.                     //set the virtual path of the file                      
    13.                     string downloadLocation = Server.MapPath("/downloads/" + fileName);  
    14.   
    15.                     //set up the file details ready for download  
    16.                     FileInfo file = new FileInfo(downloadLocation);  
    17.   
    18.                     if (file.Exists)  
    19.                     {  
    20.                         //clear the current output content from the buffer  
    21.                         Response.Clear();  
    22.   
    23.                         //add the header that specifies the default filename for the Download/SaveAs dialog  
    24.                         Response.AddHeader("Content-Disposition""attachment; filename=" + downloadLocation);  
    25.   
    26.                         //add the header that specifies the file size, so that the browser can show the download progress  
    27.                         Response.AddHeader("Content-Length", file.Length.ToString());  
    28.   
    29.                         //specify that the response is a stream that cannot be read by the client and must be downloaded  
    30.                         Response.ContentType = "application/octet-stream";  
    31.   
    32.                         //stream file to user                       
    33.                         Response.WriteFile(file.FullName);  
    34.                         Response.End();  
    35.                     }  
    36.                     else  
    37.                     {  
    38.                         //file doesnt exist  
    39.                     }  
    40.                 }  
    41.                 catch (Exception ex)  
    42.                 {  
    43.                     Label1.Text = ex.ToString();  
    44.                 }  
    45.             }  
    46.             else  
    47.             {  
    48.                 //Response.Redirect("error page");  
    49.             }  
    50.         } 
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2009 6:28 PM

All replies

  • User2089561920 posted

  • else  
  •                     {  
  •                         //file doesnt exist  
  •                     }  
  •                 }  
  •                 catch (Exception ex)  
  •                 {  
  •                     Label1.Text = ex.ToString();  
  •                 }  
  •             }  
  •             else  
  •             {  
  •                 //Response.Redirect("error page");  
  •             }  


for these 2 cases, can set label1.text to something, then see if it goes to these conditions instead of the correct download file condition

Wednesday, October 14, 2009 6:27 PM
  • User242099322 posted

    Thanks for the suggestion. I’ve added some text to the label in the catch and two else statements and nothing displays on the page. I also added some text to the end of the block of code in case it is successful and that doesn’t display either.

    This still seems to work perfectly running locally. When run in live it half works as an update is made to the DB to keep count of the number of downloads, although this is probably irrelevant.

    Have you got any other suggestions? Thanks for your help regardless.

    http://www.yourcodefactory.com/forum

    Thursday, October 15, 2009 5:24 PM
  • User2089561920 posted

    hmm this is really wierd, although where exactly in your code is the update to db?

    perhaps you can do a try catch for the whole code, and change the label1.text to response.write and see if anything shows

    else take out the filedownload portion and change it to a response.write, then slowly add in code section by section and see which code screws up

    1. protected void Page_Load(object sender, EventArgs e)  
    2.         {  
    3.             //get document name from query string  
    4.             string fileName = Request.QueryString["DocName"];  
    5.   
    6.             if (fileName != null)  
    7.             {  
    8.                 try  
    9.                 {  
    10.                     List<DownloadFile> _downloadFile = DataLayer.GetDownload(fileName);                      
    11.   
    12.                     //set the virtual path of the file                      
    13.                     string downloadLocation = Server.MapPath("/downloads/" + fileName);  
    14.   
    15.                     //set up the file details ready for download  
    16.                     FileInfo file = new FileInfo(downloadLocation);  
    17.   
    18.                     if (file.Exists)  
    19.                     {  
    20.                         //clear the current output content from the buffer  
    21.                         Response.Clear();  
    22.   
    23.                         //add the header that specifies the default filename for the Download/SaveAs dialog  
    24.                         Response.AddHeader("Content-Disposition""attachment; filename=" + downloadLocation);  
    25.   
    26.                         //add the header that specifies the file size, so that the browser can show the download progress  
    27.                         Response.AddHeader("Content-Length", file.Length.ToString());  
    28.   
    29.                         //specify that the response is a stream that cannot be read by the client and must be downloaded  
    30.                         Response.ContentType = "application/octet-stream";  
    31.   
    32.                         //stream file to user                       
    33.                         Response.WriteFile(file.FullName);  
    34.                         Response.End();  
    35.                     }  
    36.                     else  
    37.                     {  
    38.                         //file doesnt exist  
    39.                     }  
    40.                 }  
    41.                 catch (Exception ex)  
    42.                 {  
    43.                     Label1.Text = ex.ToString();  
    44.                 }  
    45.             }  
    46.             else  
    47.             {  
    48.                 //Response.Redirect("error page");  
    49.             }  
    50.         } 
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2009 6:28 PM
  • User242099322 posted

    Ok cool, thanks for your help. The update is made in another class in my data layer. I will try what you have suggested; if that doesn’t work I will try something different altogether. If I work out what the problem was I will let you know. Thanks for your help.

    Friday, October 16, 2009 3:58 AM