I have the following situation, I need to make an XML file available to be viewed in browser , without the user having to download the file, I do this by saving the file in a directory and then I send the directory of this file and the browser opens as the example below:
public string getXml(int entradaId)
{
try
{
var entrada = ctx.Entradas.Find(entradaId);
string xml = entrada.Xml;
var uploadPath = Server.MapPath("~/Content/Uploads");
string caminhoArquivo = Path.Combine(@uploadPath, entrada.ChaveNota + "-nfe.xml");
StreamWriter sw = new StreamWriter(caminhoArquivo);
sw.Write(xml);
sw.Flush();
sw.Close();
return "/Content/Uploads/" + entrada.ChaveNota + "-nfe.xml";
}
catch (Exception)
{
return "Xml Sem Entrada";
throw;
}
}
This works, but I believe it is not right. Well I end up not deleting the file and this will fill the folder with some time.
I need some way to return this XML to the Browser open as a file and make the preview available as an example .
But without my having to save the physical file.