This is a migrated thread and some comments may be shown as answers.

Downloading Large files

1 Answer 236 Views
FileExplorer
This is a migrated thread and some comments may be shown as answers.
Mevlish
Top achievements
Rank 2
Mevlish asked on 21 Sep 2011, 05:16 PM
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"/>

        

1 Answer, 1 is accepted

Sort by
0
Mevlish
Top achievements
Rank 2
answered on 04 Oct 2011, 09:18 AM
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
    }
}  
Tags
FileExplorer
Asked by
Mevlish
Top achievements
Rank 2
Answers by
Mevlish
Top achievements
Rank 2
Share this question
or