Advertisement

Latest Post

Thursday, 8 September 2016

Download file Asp.Net MVC using FileResult

public ActionResult ExportHistoricDeviceEnrol()
        {

            string fileName = ConfigurationManager.AppSettings.Get("CSVExportHistoricDeviceFileName").ToString();
            string filePath = System.Web.HttpContext.Current.Server.MapPath("/UploadedFiles/BulkUploadTemplates/HistoricDevicesEnrol/"+ fileName);          

            byte[] fileBytes = ConvertFiletobyte(filePath);
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
         

        }

        public byte[] ConvertFiletobyte(string filePath)
        {
            System.IO.FileStream filesystem = System.IO.File.OpenRead(filePath);
            byte[] bypeResult = new byte[filesystem.Length];
            Int64 length = filesystem.Read(bypeResult, 0, bypeResult.Length);
            if (length != filesystem.Length)
                throw new System.IO.IOException(s);
            return bypeResult;

        }

Government Jobs