核心代码:
var stream = new MemoryStream(); var writer = new StreamWriter(stream); //写入你的文件内容,我这里是一个字符串 writer.Write(sql); writer.Flush(); stream.Position = 0; //设定文件类型 var actionresult = new FileStreamResult(stream, new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("text/txt")); //设定文件名称 actionresult.FileDownloadName = "CreateTableSql.txt"; return actionresult;