????????????????????????<a>?????href?????????????????????????????????????????????
?????????????????
?????????????????????????????????
?????????????????????????<a>???
????<a href="~/Home/download">Click to get file</a>
????Home?controller??download?action??
?????????????Щ???????????
????<a href="~/Home/download?id=1">Click to get file</a>
??????????
??????1??????filestream
????public FileStreamResult download()
????{
????string fileName = "aaa.txt";//??????????????
????string filePath = Server.MapPath("~/Document/123.txt");//·??
????return File(new FileStream(filePath?? FileMode.Open)?? "text/plain"??
????fileName);
????}
???????У?“text/plain”?????MIME????
??????2??????file
????public FileResult download()
????{
????string filePath = Server.MapPath("~/Document/123.txt");//·??
????return File(filePath?? "text/plain"?? "welcome.txt"); //welcome.txt??????????????
????}
??????3??TransmitFile????
1  public void download()
2 {
3       string fileName = "aaa.txt";//??????????????
4       string filePath = Server.MapPath("~/Document/123.txt");//·??
5       FileInfo fileinfo = new FileInfo(filePath);
6             Response.Clear();         //????????????е????????????
7             Response.ClearContent();  //????????????е????????????
8             Response.ClearHeaders();  //????????????е??????
9             Response.Buffer = true;   //???????????????????????????????????????
10             Response.AddHeader("Content-Disposition"?? "attachment;filename=" + fileName);
11             Response.AddHeader("Content-Length"??fileinfo.Length.ToString());
12             Response.AddHeader("Content-Transfer-Encoding"?? "binary");
13             Response.ContentType = "application/unknow";  //???????????????? HTTP MIME ????
14             Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); //???????????????? HTTP ?????
15             Response.TransmitFile(filePath);
16             Response.End();
17 }
??????4??Response???????
1 public void download()
2         {
3             string fileName = "aaa.txt";//??????????????
4             string filePath = Server.MapPath("~/Document/123.txt");//·??
5
6             System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
7             if (fileInfo.Exists == true)
8             {
9                 const long ChunkSize = 102400;//100K ??ζ???????????100K???????????????????????
10                 byte[] buffer = new byte[ChunkSize];
11
12                 Response.Clear();
13                 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
14                 long dataLengthToRead = iStream.Length;//??????????????С
15                 Response.ContentType = "application/octet-stream";
16                 Response.AddHeader("Content-Disposition"?? "attachment; filename=" + HttpUtility.UrlEncode(fileName));
17                 while (dataLengthToRead > 0 && Response.IsClientConnected)
18                 {
19                     int lengthRead = iStream.Read(buffer?? 0?? Convert.ToInt32(ChunkSize));//??????С
20                     Response.OutputStream.Write(buffer?? 0?? lengthRead);
21                     Response.Flush();
22                     dataLengthToRead = dataLengthToRead - lengthRead;
23                 }
24                 Response.Close();
25             }
26         }
??????????????<input>???????ajax?????ú??????????????????????????????????????<a>??????????????<input>+ajax??