Example #1
0
        public HttpResponseMessage DownloadJson(string dataType, string lang)
        {
            var jsonResult   = string.Empty;
            var fileNameDate = string.Format("{0}{1}{2}",
                                             DateTime.Now.Year.ToString(),
                                             DateTime.Now.Month.ToString().PadLeft(2, '0'),
                                             DateTime.Now.Day.ToString().PadLeft(2, '0'));
            var fileName = string.Format(dataType + "_{0}.json", fileNameDate);

            byte[] outputBuffer        = null;
            string resultString        = string.Empty;
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            var json = string.Empty;

            switch (dataType)
            {
            case "MedicinalIngredient":
                IMedicinalIngredientRepository miPlaceholder = new MedicinalIngredientRepository();
                var medicinalIngredient = miPlaceholder.GetAllMedicinal(lang);
                if (medicinalIngredient != null)
                {
                    json = JsonConvert.SerializeObject(medicinalIngredient);
                }
                break;

            case "NonMedicinalIngredient":
                INonMedicinalIngredientRepository nmiPlaceholder = new NonMedicinalIngredientRepository();
                var nonMedicinalIngredient = nmiPlaceholder.GetAllNonMedicinal(lang);
                if (nonMedicinalIngredient != null)
                {
                    json = JsonConvert.SerializeObject(nonMedicinalIngredient);
                }
                break;
            }


            if (!string.IsNullOrWhiteSpace(json))
            {
                using (var stream = new MemoryStream())
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.WriteLine(json);
                        // writer.Flush();
                        outputBuffer = stream.ToArray();
                        resultString = Encoding.UTF8.GetString(outputBuffer, 0, outputBuffer.Length);
                    }
                }
                result.Content = new StringContent(resultString);
                result.Content.Headers.ContentType        = new MediaTypeHeaderValue("application/json");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName
                };
            }
            return(result);
        }
Example #2
0
        public HttpResponseMessage DownloadXml(string dataType, string lang)
        {
            var jsonResult   = string.Empty;
            var fileNameDate = string.Format("{0}{1}{2}",
                                             DateTime.Now.Year.ToString(),
                                             DateTime.Now.Month.ToString().PadLeft(2, '0'),
                                             DateTime.Now.Day.ToString().PadLeft(2, '0'));
            var fileName = string.Format(dataType + "_{0}.xml", fileNameDate);

            byte[] outputBuffer        = null;
            string resultString        = string.Empty;
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            var xmlString = string.Empty;

            switch (dataType)
            {
            case "MedicinalIngredient":
                IMedicinalIngredientRepository miPlaceholder = new MedicinalIngredientRepository();
                var medicinalIngredient = miPlaceholder.GetAllMedicinal(lang);
                if (medicinalIngredient != null)
                {
                    xmlString = GetXMLFromObject(medicinalIngredient);
                }
                break;

            case "NonMedicinalIngredient":
                INonMedicinalIngredientRepository nmiPlaceholder = new NonMedicinalIngredientRepository();
                var nonMedicinalIngredient = nmiPlaceholder.GetAllNonMedicinal(lang);
                if (nonMedicinalIngredient != null)
                {
                    xmlString = GetXMLFromObject(nonMedicinalIngredient);
                }
                break;
            }

            if (!string.IsNullOrWhiteSpace(xmlString))
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlString);
                using (var stream = new MemoryStream())
                {
                    using (var writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8))
                    {
                        writer.Formatting  = System.Xml.Formatting.Indented;
                        writer.Indentation = 4;

                        // Write the XML declaration.
                        doc.WriteTo(writer);
                        //writer.WriteFullEndElement();
                        // writer.Flush();
                        outputBuffer = stream.ToArray();
                        resultString = Encoding.UTF8.GetString(outputBuffer, 0, outputBuffer.Length);
                        writer.Close();
                    }
                }
                result.Content = new StringContent(resultString);
                result.Content.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName
                };
            }

            return(result);
        }