public IActionResult Import(FileUpload fileUpload) { var file = fileUpload.FormFile; using (var stream = file.OpenReadStream()) { StreamReader reader = new StreamReader(stream); string export = reader.ReadToEnd(); var operation = OperationExport.Import(export); return(RedirectToAction("Edit", new { id = operation.Id })); } }
public IActionResult Export(Guid id) { var op = dataContext.Operations.Where(o => o.Id == id).FirstOrDefault(); var export = OperationExport.GetExport(op); var output = JsonConvert.SerializeObject(export); var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(output); writer.Flush(); stream.Position = 0; return(File(stream, "application/json", op.Name.Replace(" ", "_") + ".json")); }
public IActionResult ExportAll() { var operations = dataContext.Operations.ToList(); System.IO.File.Delete("export.zip"); var zip = ZipFile.Open("export.zip", ZipArchiveMode.Create); foreach (var op in operations) { var export = OperationExport.GetExport(op); var output = JsonConvert.SerializeObject(export); var fileName = op.Name.Replace(" ", "_") + ".json"; System.IO.File.WriteAllText(fileName, output); zip.CreateEntryFromFile(fileName, fileName); System.IO.File.Delete(fileName); } zip.Dispose(); var stream = System.IO.File.Open("export.zip", FileMode.Open); return(File(stream, "application/zip", "export.zip")); }