Ejemplo n.º 1
0
        /// <summary>
        /// Processes and writes the CSV or Fixed format.
        /// </summary>
        /// <returns>CSV or Fixed file</returns>
        private byte[] WriteCsvToMemory(IEnumerable <StationData> records, string[] columns, DownloadFormatEnum downloadFormat)
        {
            // Use the streams and writers to generate the file
            using (var memoryStream = new MemoryStream())
                using (var streamWriter = new StreamWriter(memoryStream))
                    using (var csvWriter = new CsvWriter(streamWriter))
                    {
                        // Remove delimeter for fixed format
                        // Remove header row for fixed format
                        if (downloadFormat == DownloadFormatEnum.Fixed)
                        {
                            csvWriter.Configuration.Delimiter       = "";
                            csvWriter.Configuration.TrimOptions     = CsvHelper.Configuration.TrimOptions.None;
                            csvWriter.Configuration.ShouldQuote     = (field, context) => false;
                            csvWriter.Configuration.HasHeaderRecord = false;
                        }

                        // Configure the columns to write to the file
                        var map = new CsvHelper.Configuration.DefaultClassMap <StationData>();
                        foreach (var column in columns)
                        {
                            var property = typeof(StationData).GetProperty(column.Replace("_", ""));

                            // Use the FixedWidthConverter for the fixed format
                            if (downloadFormat == DownloadFormatEnum.Fixed)
                            {
                                map.Map(typeof(StationData), property).TypeConverter <FixedWidthConverter>();
                            }
                            else
                            {
                                map.Map(typeof(StationData), property);
                            }
                        }
                        csvWriter.Configuration.RegisterClassMap(map);

                        // Write the records and flush
                        csvWriter.WriteRecords(records);
                        streamWriter.Flush();
                        return(memoryStream.ToArray());
                    }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> MeteorologicalDownload(int id, DateTime beginDate, DateTime endDate, DownloadFormatEnum downloadFormat, string variables)
        {
            // Validation:
            // 1. Must have 1 variable selected
            // 2. endDate must come after beginDate
            // 3. Timespan between beginDate and endDate must be 31 or less days
            if (variables == null || beginDate > endDate || (endDate - beginDate).Days > 31)
            {
                return(View("Error", new ErrorDto {
                    StatusCode = 400
                }));
            }

            // Get station Info
            var stationInfo = await _stationService.GetStationInfo(id);

            // Get data for the file
            var data = await _stationDataService.MeteorologicalDownloadData(id, beginDate, endDate);

            // File extension
            var extension = "csv";

            if (downloadFormat == DownloadFormatEnum.Fixed)
            {
                extension = "dat";
            }

            // Add Timestamp column for CSV
            if (downloadFormat == DownloadFormatEnum.CSV)
            {
                variables = "Ts," + variables;
            }

            // Create file name
            var fileName = stationInfo.DisplayName.Replace(" ", "_")
                           + "_" + beginDate.ToString(Constant.DATE_FILE_FORMAT)
                           + "_" + endDate.ToString(Constant.DATE_FILE_FORMAT)
                           + "." + extension;

            // Create the memory stream for the file
            var memoryStream = new MemoryStream(WriteCsvToMemory(data, variables.Split(","), downloadFormat));

            // Return the file stream
            return(new FileStreamResult(memoryStream, "text/csv")
            {
                FileDownloadName = fileName
            });
        }