ASP.Net MVC ??????????????
???????????? ???????[ 2016/5/19 10:13:44 ] ??????????????????? .NET
????????????????????????<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??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11