Downloading Large files

2 posts, 0 answers
  1. Mevlish
    Mevlish avatar
    2 posts
    Member since:
    Oct 2010

    Posted 21 Sep 2011 Link to this post

    Hi,

    I am using a file explorer control. under the open event of the right click menu I am forcing the files to be downloaded. The Download works fine for small files. But When I download a file which is around 300mb in size I get an error. There is no error code. I get the Internet explorer error message saying the page cannot be displayed.

    I assume that it might be a time out issue.

    Can someone help me overcome this issue.

    Thanks 

    My code is as follow
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    in .aspx page 

    <script type="text/javascript" >

        function OnClientFileOpen(sender, args) 
            {
                if (!args.get_item().isDirectory()) 
                {
                    var href = args.get_item().get_path();
                    window.location.href = href;
                    args.set_cancel(true);
    var item = args.get_item();
                    var requestfile = "Handler.ashx?path=" + item.get_url();
                    document.location = requestfile;
                   
                }
            }
    </script>

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Handler.ashx


    <%@ WebHandler Language="C#" Class="Telerik.Web.Examples.FileExplorer.FilterAndDownloadFiles.Handler" %>

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Text;
    using Telerik.Web.UI;


    namespace Telerik.Web.Examples.FileExplorer.FilterAndDownloadFiles
    {
        [RadCompressionSettings(HttpCompression = CompressionType.None)] // Disable RadCompression for this page ;
        public class Handler : IHttpHandler
        {
            #region IHttpHandler Members


            private HttpContext _context;
            private HttpContext Context
            {
                get
                {
                    return _context;
                }
                set
                {
                    _context = value;
                }
            }




            public void ProcessRequest(HttpContext context)
            {
                Context = context;
                string filePath = context.Request.QueryString["path"];
                filePath = context.Server.MapPath(filePath);


                if (filePath == null)
                {
                    return;
                }


                System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
                System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);


                byte[] bytes = new byte[streamReader.BaseStream.Length];


                br.Read(bytes, 0, (int)streamReader.BaseStream.Length);


                if (bytes == null)
                {
                    return;
                }


                streamReader.Close();
                br.Close();
                string extension = System.IO.Path.GetExtension(filePath);
                string fileName = System.IO.Path.GetFileName(filePath);
                //context.Response.TransmitFile(fileName);
                WriteFile(bytes, fileName, "application/octet-stream", Context.Response);
            }


            /// <summary>
            /// Sends a byte array to the client
            /// </summary>
            /// <param name="content">binary file content</param>
            /// <param name="fileName">the filename to be sent to the client</param>
            /// <param name="contentType">the file content type</param>
            private void WriteFile(byte[] content, string fileName, string contentType, HttpResponse response)
            {
                response.Buffer = true;
                response.Clear();
                response.ContentType = contentType;


                response.AddHeader("content-disposition", "attachment; filename=" + fileName);


                response.BinaryWrite(content);
                response.Flush();
                response.End();
            }


            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }


            #endregion
        }
    }


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Web.config

        <httpRuntime executionTimeout="1200" maxRequestLength="1048576" requestLengthDiskThreshold="80" 
                     useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" 
                     appRequestQueueLimit="5000" enableKernelOutputCache="true" enableVersionHeader="true" 
                     requireRootedSaveAsPath="true" enable="true" shutdownTimeout="90" delayNotificationTimeout="5"
                     waitChangeNotification="0" maxWaitChangeNotification="0" enableHeaderChecking="true" 
                     sendCacheControlHeader="true" apartmentThreading="false"/>

            
  2. Mevlish
    Mevlish avatar
    2 posts
    Member since:
    Oct 2010

    Posted 04 Oct 2011 Link to this post

    Hi,

    I changed my file handler.ashx file to the following and it has resolved the issue. I hope this post will help you. 

    Regards
    Mev
    <%@ WebHandler Language="C#" Class="Telerik.Web.Examples.FileExplorer.FilterAndDownloadFiles.Handler" %>
     
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Text;
    using Telerik.Web.UI;
     
    namespace Telerik.Web.Examples.FileExplorer.FilterAndDownloadFiles
    {
        [RadCompressionSettings(HttpCompression = CompressionType.None)] // Disable RadCompression for this page ;
        public class Handler : IHttpHandler
        {
            #region IHttpHandler Members
     
            private HttpContext _context;
            private HttpContext Context
            {
                get
                {
                    return _context;
                }
                set
                {
                    _context = value;
                }
            }
     
     
            public void ProcessRequest(HttpContext context)
            {
                try
                {
                Context = context;
                string filePath = context.Request.QueryString["path"];
                filePath = context.Server.MapPath(filePath);
     
                if (filePath == null)
                {
                    return;
                }
     
                System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
                System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);
     
                byte[] bytes = new byte[streamReader.BaseStream.Length];
     
                br.Read(bytes, 0, (int)streamReader.BaseStream.Length);
     
                if (bytes == null)
                {
                    return;
                }
     
                streamReader.Close();
                br.Close();
                string extension = System.IO.Path.GetExtension(filePath);
                string fileName = System.IO.Path.GetFileName(filePath);
                context.Response.ContentType = "application/octet-stream";
                context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                context.Response.TransmitFile(filePath);
                context.Response.Flush();
                context.Response.End();
                }
                catch (Exception ex)
                {
                   context.Response.ContentType = "text/plain";
                    context.Response.Write("Error :" + ex.Message);
                }
            }
     
             
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
     
            #endregion
        }
    }  
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top