实现多附件的上传,俗话说:语言不如文字,文字不如图形。先传个页面看看

可以点击“添加”按钮任意添加上传文件的个数,也可以通过“取消”按钮取消多个文件的上传。这样可以一次上传N个附件,而不需要每上传一个附件点击一次上传按钮。达到了一次上传,一次成功的效果。
1、JS代码
1
function addFileControl()
2
{
3
var str='<input type=file name=File size=40>'
4
document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
5
}
6
7
function delFile()
8
{
9
var el = document.getElementById("FileCollection");
10
if (el.hasChildNodes())
11
el.removeChild(el.lastChild);
12
}
13
function addFileControl()2
{3
var str='<input type=file name=File size=40>'4
document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)5
}6
7
function delFile()8
{9
var el = document.getElementById("FileCollection");10
if (el.hasChildNodes())11
el.removeChild(el.lastChild);12
} 13

2. 前台代码
1
<p id="FileCollection">
2
<input type="file" name="File" id="file" size="40" />
3
<p>
4
<%--<input type =image onclick="addFileControl()" src="../Images/tianjia.gif" value="增加"/>--%>
5
<input type="button" value="增加" onclick="addFileControl()" class="button" />
6
7
<input type="button" value="取消" onclick="delFile()" class="button" /></p>
8
<p id="FileCollection">2
<input type="file" name="File" id="file" size="40" />3
<p>4
<%--<input type =image onclick="addFileControl()" src="../Images/tianjia.gif" value="增加"/>--%>5
<input type="button" value="增加" onclick="addFileControl()" class="button" />6
7
<input type="button" value="取消" onclick="delFile()" class="button" /></p>8

3、后台CS代码
1
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
2
int fileCount;
3
string Extension = "";
4
string SavedName = "";
5
string upname = "";
6
string savedpath = "";
7
string realname="";
8
string virtualname = "";
9
string path = "";
10
string OppPath = "UploadFile";
11
try
12
{
13
if (!Directory.Exists(Server.MapPath(OppPath)))
14
Directory.CreateDirectory(Server.MapPath(OppPath));
15
16
OppPath += "/";
17
string AbsPath = Server.MapPath(OppPath);
18
19
for (fileCount = 0; fileCount < files.Count; fileCount++)
20
{
21
22
//定义访问客户端上传文件的对象
23
System.Web.HttpPostedFile postedFile = files[fileCount];
24
string fileName, fileExtension;
25
fileName = System.IO.Path.GetFileName(postedFile.FileName);
26
if (fileName != string.Empty)
27
{
28
Extension = System.IO.Path.GetExtension(fileName);
29
////取上传文件的文件名
30
31
upname = System.IO.Path.GetFileName(fileName);
32
//系统自动产生的日期型文件名
33
34
35
SavedName = string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now);
36
////把文件保存到UploadFile文件中
37
38
39
postedFile.SaveAs(AbsPath+SavedName + upname);
40
string strJS = "<script>";
41
strJS = strJS + "parent.HtmlEdit.focus();";
42
strJS = strJS + "var range = parent.HtmlEdit.document.selection.createRange();";
43
strJS = strJS + "range.pasteHTML('<a href=\"" + savedpath + SavedName + upname;
44
strJS = strJS + "\">" + SavedName +"</a>');";
45
strJS = strJS + "parent.HtmlEdit.focus();";
46
strJS = strJS + "</script>";
47
Response.Write(strJS);
48
virtualname +=OppPath+ SavedName + upname + ",";
49
realname += upname + ",";
50
path = savedpath;
51
}
52
}
53
54
}
55
catch (System.Exception error)
56
{
57
throw error;
58
}
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;2
int fileCount;3
string Extension = "";4
string SavedName = "";5
string upname = "";6
string savedpath = "";7
string realname="";8
string virtualname = "";9
string path = "";10
string OppPath = "UploadFile";11
try12
{13
if (!Directory.Exists(Server.MapPath(OppPath)))14
Directory.CreateDirectory(Server.MapPath(OppPath));15

16
OppPath += "/";17
string AbsPath = Server.MapPath(OppPath);18

19
for (fileCount = 0; fileCount < files.Count; fileCount++)20
{21

22
//定义访问客户端上传文件的对象23
System.Web.HttpPostedFile postedFile = files[fileCount];24
string fileName, fileExtension;25
fileName = System.IO.Path.GetFileName(postedFile.FileName);26
if (fileName != string.Empty)27
{28
Extension = System.IO.Path.GetExtension(fileName);29
////取上传文件的文件名30

31
upname = System.IO.Path.GetFileName(fileName);32
//系统自动产生的日期型文件名33

34

35
SavedName = string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now);36
////把文件保存到UploadFile文件中37

38

39
postedFile.SaveAs(AbsPath+SavedName + upname);40
string strJS = "<script>";41
strJS = strJS + "parent.HtmlEdit.focus();";42
strJS = strJS + "var range = parent.HtmlEdit.document.selection.createRange();";43
strJS = strJS + "range.pasteHTML('<a href=\"" + savedpath + SavedName + upname;44
strJS = strJS + "\">" + SavedName +"</a>');";45
strJS = strJS + "parent.HtmlEdit.focus();";46
strJS = strJS + "</script>";47
Response.Write(strJS);48
virtualname +=OppPath+ SavedName + upname + ",";49
realname += upname + ",";50
path = savedpath;51
}52
}53
54
}55
catch (System.Exception error)56
{57
throw error;58
} 1
public int File_pre_Appendix(int i, int j,string appendixname,string appendixpath)
2
{
3
if ((i != 0) && (appendixname != ""))
4
{
5
//对文件的附件进行存储
6
string[] sArray = appendixname.Split(',');
7
foreach (string strArr in sArray)
8
{
9
10
if (strArr != "")
11
{
12
//取上传文件的文件名
13
file.GetID = i;
14
file.AppendixNmae = OppPath + strArr.ToString().Trim();
15
file.AppendixAddress = appendixpath.Trim();
16
file.UserID = UserID;
17
file.AppendixTypeID = j;
18
filedb.insertAppendix(file);
19
}
20
21
}
22
return 1;
23
}
24
else
25
{
26
return 0;
27
}
28
}
29
//获取附件表中的附件名称(路径)
30
string OppPath =fileDB.GetFile_AppendixPath((object)AppendixID);
31
//获取绝对路径
32
string AbsPath = Page.Server.MapPath(OppPath );
33
if (System.IO.File.Exists(AbsPath))
34
{
35
//删除硬盘文件
36
System.IO.File.Delete(AbsPath);
37
}
38
public int File_pre_Appendix(int i, int j,string appendixname,string appendixpath)2
{3
if ((i != 0) && (appendixname != ""))4
{5
//对文件的附件进行存储6
string[] sArray = appendixname.Split(',');7
foreach (string strArr in sArray)8
{9

10
if (strArr != "")11
{12
//取上传文件的文件名13
file.GetID = i;14
file.AppendixNmae = OppPath + strArr.ToString().Trim();15
file.AppendixAddress = appendixpath.Trim();16
file.UserID = UserID;17
file.AppendixTypeID = j;18
filedb.insertAppendix(file);19
}20

21
}22
return 1;23
}24
else25
{26
return 0;27
}28
}29
//获取附件表中的附件名称(路径)30
string OppPath =fileDB.GetFile_AppendixPath((object)AppendixID);31
//获取绝对路径32
string AbsPath = Page.Server.MapPath(OppPath );33
if (System.IO.File.Exists(AbsPath))34
{35
//删除硬盘文件36
System.IO.File.Delete(AbsPath);37
} 38



浙公网安备 33010602011771号