这是我的代码。
如果没有在服务器上进行物理保存,我可以这样做吗?
谢谢!
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();
答案 0 :(得分: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"
};
其中content是包含数据的byte []你想要放入文件