0

这是我的代码。

我可以在不物理保存服务器的情况下做到这一点吗?

谢谢!!

StringBuilder text = new StringBuilder();
string sFileName = "file.txt";

using (System.IO.StreamWriter SW = new System.IO.StreamWriter(Server.MapPath("docs\\" + sFileName), false, Encoding.ASCII))
{
    SW.WriteLine("test");
    SW.Close();
}

System.IO.FileStream fs = null;
fs = System.IO.File.Open(Server.MapPath("docs\\" + sFileName), System.IO.FileMode.Open);
byte[] btFile = new byte[fs.Length];
fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
fs.Close();
Response.AddHeader("Content-disposition", "attachment; filename=" + sFileName);
Response.ContentType = "text/plain";
Response.BinaryWrite(btFile);
Response.End();
4

1 回答 1

1
var result = new HttpResponseMessage(HttpStatusCode.OK)
             {
                 Content = new ByteArrayContent(content)
             };

//a text file is actually an octet-stream (pdf, etc)
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
//we used attachment to force download
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                                           {
                                               FileName = "tets.txt"
                                           };

其中内容是包含数据的字节 [] 您要放入文件中

于 2013-10-24T11:00:00.543 回答