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"
                                           };

content は byte[] で、ファイルに入れたいデータを含んでいます

于 2013-10-24T11:00:00.543 に答える