当前位置:网站首页>asp. Net file download demo and related problems

asp. Net file download demo and related problems

2022-06-23 07:03:00 Wool leek

Download the demo( Total size of displayable files , And solves the problem that the download ends automatically after the download is completed )

 // Download in blocks 
        private void downLoadPackage()
        {
            System.Web.HttpServerUtility server = System.Web.HttpContext.Current.Server;
            string fileName = "aaa.apk";
            // route 
            string filePath = server.MapPath("DownLoadPackage/aaa.apk");

            if (!File.Exists(filePath)) return;
            
            FileInfo info = new FileInfo(filePath);
            // Specify block size    
            long chunkSize = 4096;
            // Build a 4K The buffer    
            byte[] buffer = new byte[chunkSize];
            // The number of bytes left    
            long dataToRead = 0;
            FileStream stream = null;
            try
            {
                // Open file    
                stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                
                dataToRead = stream.Length;

                // add to Http head    
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(fileName));
                HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());

                while (dataToRead > 0)
                {
                    if (HttpContext.Current.Response.IsClientConnected)
                    {
                        int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
                        HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
                        HttpContext.Current.Response.Flush();
                        HttpContext.Current.Response.Clear();
                        dataToRead -= length;
                    }
                    else
                    {
                        // prevent client Lose connection    
                        dataToRead = -1;
                    }
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write("Error:" + ex.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                HttpContext.Current.Response.Close();
            }
        }

iis Cannot download on .apk and .ipa The problem of

single click “ newly build ”, Set up new MIME type ;

The extension is :.apk MIMI The type is :application/vnd.android.package-archive

The extension is :.ipa MIMI The type is :application/iphone

原网站

版权声明
本文为[Wool leek]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230531031573.html