예제 #1
0
        public void DownloadFile(string filePath)
        {
            //Stream fileStream = azureServices.GetStream(filePath);
            //fileStream.Position = 0;
            // Buffer to read 10K bytes in chunk:
            int chunkSize = 1024 * 1024;

            byte[] buffer = new Byte[chunkSize];

            // Length of the file:
            long totalLength = dataServices.GetBlobSizeInBytes(filePath);

            // Total bytes to read:
            long dataToRead      = totalLength;
            long currentPosition = 0;

            try
            {
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Utility.GetFileName(filePath));
                // Read the bytes.
                while (dataToRead > 0)
                {
                    // Verify that the client is connected.
                    if (Response.IsClientConnected)
                    {
                        // Read the data in buffer.
                        //length = fileStream.Read(buffer, 0, 10000);
                        long readLength = dataServices.GetBlobRangeToArrayByte(filePath, buffer, currentPosition, chunkSize);
                        currentPosition += readLength;
                        // Write the data to the current output stream.
                        Response.OutputStream.Write(buffer, 0, (int)readLength);

                        // Flush the data to the HTML output.
                        Response.Flush();

                        buffer     = new Byte[chunkSize];
                        dataToRead = dataToRead - readLength;
                    }
                    else
                    {
                        //prevent infinite loop if user disconnects
                        dataToRead = -1;
                    }
                }
            }
            catch (Exception ex)
            {
                // Trap the error, if any.
                Response.Write("Error : " + ex.Message);
            }
            finally
            {
                /*
                 * if (fileStream != null)
                 * {
                 *  //Close the file.
                 *  fileStream.Close();
                 * }*/
                Response.Close();
            }

            //            HttpContext.Response.ContentType = new FileStreamResult(fileStream);

            //string mime = MimeMapping.GetMimeMapping(filePath);
            //return new FileStreamResult(fileStream, mime);
        }