public HttpResponseMessage Get(bool isExcel, string file) { try { FileStream original = File.Open(Utils._storagePath + "\\" + file, FileMode.OpenOrCreate); string outputPath = Utils._storagePath + "\\Metadata_" + Path.GetFileNameWithoutExtension(file) + (isExcel ? ".xlsx" : ".csv"); if (isExcel) { byte[] content = ExportFacade.ExportToExcel(original); // write data to the file File.WriteAllBytes(outputPath, content); } else { byte[] content = ExportFacade.ExportToCsv(original); // write data to the file File.WriteAllBytes(outputPath, content); } using (var ms = new MemoryStream()) { original = File.OpenRead(outputPath); original.CopyTo(ms); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(ms.ToArray()) }; result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = "Metadata_" + Path.GetFileNameWithoutExtension(file) + (isExcel ? ".xlsx" : ".csv") }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); original.Close(); File.Delete(outputPath); return(result); } } catch (Exception exc) { throw exc; } }
/// <summary> /// Exports metadata of specified file into specified type /// </summary> public static void ExportMetadata(string filePath, int exportType) { try { //ExStart:ExportMetadataAPI filePath = Common.MapSourceFilePath(filePath); if (exportType == ExportTypes.ToExcel) { //ExStart:ExportMetadataToExcel // path to the output file string outputPath = Common.MapDestinationFilePath("metadata.xlsx"); // export to excel byte[] content = ExportFacade.ExportToExcel(filePath); // write data to the file File.WriteAllBytes(outputPath, content); //ExEnd:ExportMetadataToExcel } else if (exportType == ExportTypes.ToCSV) { //ExStart:ExportMetadataToCVS // path to the output file string outputPath = Common.MapDestinationFilePath("metadata.csv"); // export to csv byte[] content = ExportFacade.ExportToCsv(filePath); // write data to the file File.WriteAllBytes(outputPath, content); //ExEnd:ExportMetadataToCVS } else { //ExStart:ExportMetadataToDataSet // export to DataSet DataSet ds = ExportFacade.ExportToDataSet(filePath); // get first table DataTable products = ds.Tables[0]; // need to System.Data.DataSetExtension reference IEnumerable <DataRow> query = from product in products.AsEnumerable() select product; Console.WriteLine("Properties:"); foreach (DataRow p in query) { Console.Write(p.Field <string>("Metadata property")); Console.Write(": "); Console.WriteLine(p.Field <string>("Value")); } //ExEnd:ExportMetadataToDataSet } //ExEnd:ExportMetadataAPI } catch (Exception exp) { Console.WriteLine("Exception occurred: " + exp.Message); } }