1
2
protected void Button1_Click(object sender, EventArgs e)
3

{
4
5
//*************文件的下载的实现**************
6
7
8
string path = Server.MapPath("~/image/此情可待.wma");
9
10
//string path1 = Server.MapPath("~/image/PabloCastro.jpg");
11
12
13
//string path = Server.MapPath("~/image/PabloCastro.jpg");
14
System.IO.FileInfo file = new System.IO.FileInfo(path);
15
string filename = file.Name.ToString();
16
17
// clear the current output content from the buffer
18
19
Response.Clear();
20
21
// add the header that specifies the default filename for the Download/SaveAs dialog
22
23
Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + Server.UrlEncode(filename) + "\"");//ture解决名字乱码现象
24
25
26
//Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);//乱码
27
28
// add the header that specifies the file size, so that the browser
29
30
// can show the download progress
31
32
Response.AddHeader("Content-Length", file.Length.ToString());
33
34
// specify that the response is a stream that cannot be read by the
35
36
// client and must be downloaded
37
Response.ContentType = "application/octet-stream";
38
39
40
// send the file stream to the client
41
42
43
Response.WriteFile(file.FullName);
44
45
// stop the execution of this page
46
47
Response.End();
48
49
50
51
52
}

2

3



4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52
