代码之家  ›  专栏  ›  技术社区  ›  niico

在ASP中创建文本文件并下载,无需在服务器上保存。net核心MVC 2.1

  •  0
  • niico  · 技术社区  · 7 年前

    Create text file and download

    公认的答案使用:

    using (StreamWriter writer = new StreamWriter(Response.OutputStream, Encoding.UTF8)) {
      writer.Write("This is the content");
    }
    

    我需要在ASP中执行此操作。net Core 2.1 MVC-尽管在这方面不知道有什么反应。OutputStream是——我在谷歌上找不到任何帮助,也找不到其他方法。

    我该怎么做?谢谢

    3 回复  |  直到 7 年前
        1
  •  17
  •   Chris Pratt    7 年前

    ContentResult :

    return Content("This is some text.", "text/plain");
    

    这对其他“文本”内容类型也适用,如CSV:

    return Content("foo,bar,baz", "text/csv");
    

    FileResult byte[] :

    return File(Encoding.UTF8.GetBytes(text), "text/plain", "foo.txt");
    

    这个 filename Content-Disposition: attachment; filename="foo.txt" 标题。或者,您也可以返回 Content 只需手动设置此标题:

    Response.Headers.Add("Content-Disposition", "attachment; filename=\"foo.txt\"");
    return Content(text, "text/plain");
    

    最后,如果要在流中构建文本,只需返回 FileStreamResult

    return File(stream, "text/plain", "foo.txt");
    
        2
  •  8
  •   Ortund    5 年前

    在下面的代码中,您使用Response。输出流。但是,这在asp中非常有效。净,但反应。OutputStream在asp中引发错误。净核心。

    using (StreamWriter writer = new StreamWriter(Response.OutputStream, Encoding.UTF8)) {
        writer.Write("This is the content");
    }
    

    所以,请使用以下代码在asp中下载文件。净核心。

    using (MemoryStream stream = new MemoryStream())
    {
    StreamWriter objstreamwriter = new StreamWriter(stream);
    objstreamwriter.Write("This is the content");
    objstreamwriter.Flush();
    objstreamwriter.Close(); 
    return File(stream.ToArray(), "text/plain", "file.txt");
    }
    
        3
  •  3
  •   kasptom    7 年前

    A little different way

    编辑:修复了文件末尾的尾随零

    [HttpGet]
    [Route("testfile")]
    public ActionResult TestFile()
    {
        MemoryStream memoryStream = new MemoryStream();
        TextWriter tw = new StreamWriter(memoryStream);
    
        tw.WriteLine("Hello World");
        tw.Flush();
    
        var length = memoryStream.Length;
        tw.Close();
        var toWrite = new byte[length];
        Array.Copy(memoryStream.GetBuffer(), 0, toWrite, 0, length);
    
        return File(toWrite, "text/plain", "file.txt");
    }
    

    旧答案(尾随零问题)

    [HttpGet]
    [Route("testfile")]
    public ActionResult GetTestFile() {
        MemoryStream memoryStream = new MemoryStream();
        TextWriter tw = new StreamWriter(memoryStream);
    
        tw.WriteLine("Hello World");
        tw.Flush();
        tw.Close();
    
        return File(memoryStream.GetBuffer(), "text/plain", "file.txt");
    }
    
        4
  •  1
  •   zuluk    6 年前
    public ActionResult Create(Information information)
    {
       var byteArray = Encoding.ASCII.GetBytes(information.FirstName + "" + information.Surname + "" + information.DOB + "" + information.Email + " " + information.Tel);
       var stream = new MemoryStream(byteArray);
    
       return File(stream, "text/plain", "your_file_name.txt");
    }