ASP.NET MVC4 利用uploadify.js多文件上传

页面代码:

1.引入js和css文件

?

1

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

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

 "~/Scripts/uploadify/uploadify.css" rel="external nofollow" rel="stylesheet" />

2.body内代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

  <body style="background: url(../../Images/bg.jpg) no-repeat; background-size: 1600px; width: 1600px; height: 700px; ">

<h2 style="text-align:center;">ASP .NET MVC4 多文件文件上传实例h2>

<form id="form1">

<div>

<input type="file" id="myfile" name="myfile" />

div>

<div>

<a class="btn" href="javascript:$('#myfile').uploadify('upload');" rel="external nofollow" >上传第一个a>

<a class="btn" href="javascript:$('#myfile').uploadify('upload','*');" rel="external nofollow" >上传队列a>

<a class="btn" href="javascript:$('#myfile').uploadify('cancel');" rel="external nofollow" >取消第一个a>

<a class="btn" href="javascript:$('#myfile').uploadify('cancel', '*');" rel="external nofollow" >取消队列a>

div>

form>

<div id="upList">

<div id="filelist">

<h3>文件列表h3>

div>

<div id="lineDiv">div>

<div id="imglist">

<h3>图片列表h3>

div>

div>

body>

后台代码:

?

1

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

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

public ActionResult loadFileInfo()

{

StringBuilder sb = new StringBuilder();

DirectoryInfo theFolder = new DirectoryInfo(Server.MapPath("~/UploadFile/"));

DirectoryInfo[] dirInfo = theFolder.GetDirectories();

//遍历文件夹

foreach (DirectoryInfo NextFolder in dirInfo)

{

FileInfo[] fileInfo = NextFolder.GetFiles();

//遍历文件

foreach (FileInfo NextFile in fileInfo)

{

string exStr = NextFile.Extension;

string str = NextFile.Name;

if (exStr == ".zip" || exStr == ".7z" || exStr == ".rar" || exStr.ToLower() == ".rars")

{

sb.Append("

" + str + "

");

}

else if (exStr == ".doc" || exStr == ".docx")

{

sb.Append("

" + str + "

");

}

else if (exStr == ".ppt" || exStr == ".pptx")

{

sb.Append("

" + str + "

");

}

else if (exStr == ".xlsx" || exStr == ".xls" || exStr == ".XLS")

{

sb.Append("

" + str + "

");

}

else if (exStr == ".pdf")

{

sb.Append("

" + str + "

");

}

else if (exStr == ".js" || exStr == ".JS")

{

sb.Append("

" + str + "

");

}

else if (exStr == ".html" || exStr == ".HTML")

{

sb.Append("

" + str + "

");

}

else if (exStr == ".txt" || exStr == ".TXT")

{

sb.Append("

" + str + "

");

}

else if (exStr == ".mp3" || exStr == ".wmv" || exStr == ".aac")

{

sb.Append("

" + str + "

");

}

else if (exStr == ".avi" || exStr == ".mov" || exStr == ".mp4" || exStr == ".ram" || exStr == ".flv")

{

sb.Append("

" + str + "

");

}

else {

sb.Append("

" + str + "

");

}

}

}

return Content(sb.ToString());

}

public ActionResult loadImgInfo()

{

StringBuilder sb = new StringBuilder();

DirectoryInfo theFolder = new DirectoryInfo(Server.MapPath("~/UploadImg/"));

DirectoryInfo[] dirInfo = theFolder.GetDirectories();

//遍历文件夹

foreach (DirectoryInfo NextFolder in dirInfo)

{

FileInfo[] fileInfo = NextFolder.GetFiles();

//遍历文件

foreach (FileInfo NextFile in fileInfo)

{

string str = NextFile.Name;

sb.Append("

" + str + "

");

}

}

return Content(sb.ToString());

}

public ActionResult UploadFile()

{

string filepath = "";

bool fileOK = false;

//判断是否已经选择上传文件

HttpPostedFileBase file = Request.Files["myfile"];

if (file != null && file.ContentLength > 0)

{

String fileExtension = System.IO.Path.GetExtension(file.FileName).ToLower();

//判断是否为图片类型

String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };

for (int i = 0; i < allowedExtensions.Length; i++)

{

if (fileExtension == allowedExtensions[i])

{

fileOK = true;

}

}

if (fileOK)

{

//设置上传目录

string path = Server.MapPath("~/UploadImg/Img/");

if (!Directory.Exists(path))

Directory.CreateDirectory(path);

string filenNamer = file.FileName;

//文件路径

filepath = path + filenNamer;

file.SaveAs(filepath);

return RedirectToAction("Upload", "Home");

}

else

{

//设置上传目录

string path = Server.MapPath("~/UploadFile/File/");

if (!Directory.Exists(path))

Directory.CreateDirectory(path);

//不为图片类型的文件存入到File目录中

string filenNamer = file.FileName;

//文件路径

filepath = path + filenNamer;

file.SaveAs(filepath);

return RedirectToAction("Upload", "Home");

}

}

else

{

var script = String.Format("", Url.Action("Upload"));

return Content(script, "text/html");

}

}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

你可能感兴趣的:(IIS相关)