HTML:
<form id="form1" runat="server" enctype="multipart/form-data">
<input id="FileTI1" name="FileTI1" type="file" style=" 300px;" /><br />
<div id="divTI">
</div>
<input id="btnAddAttTI1" type="button" value="添加附件" onclick="btnAddAttTI();" />
JS:
var ti = 1;
function btnAddAttTI() {
ti += 1;
$("#divTI").append("<div id="divTI" + ti + ""><input id="FileTI" + ti + "" name="FileTI" + ti + "" type="file" style=" 300px;" /><img src='images/del.gif' style='cursor: pointer;' width='15' height='15' onclick="deleteTI('" + ti + "')" alt='删除附件' /> <br /></div>")
}
function deleteTI(tiid) {
$("#divTI" + tiid + "").remove();
}
c#
/// <summary>
/// 上传附件
/// </summary>
/// <returns></returns>
private bool UploadAtt(string pId, string htNo)
{
bool R = false;
HttpFileCollection files = HttpContext.Current.Request.Files;
string pathML = "", pathTI = "", pathCK = "", pathZB = "", pathXHT = "", pathYS = "";
string dML = "", dTI = "", dCK = "", dZB = "", dXHT = "", dYS = "";
#region 创建保存附件目录
if (files.Count != 0)
{
string path = Server.MapPath("UploadFile");
pathML = path + "/" + pid + "/" + htno + "/ml";
dML = "BusinessUploadFile/" + pid + "/" + htno + "/ml";
//创建目录
if (!Directory.Exists(path + "/" + pid + "/" + htno + "/ml"))
{
Directory.CreateDirectory(path + "/" + pid + "/" + htno + "/ml");
}
}
#endregion
int BZ = 0;
for (int iFile = 0; iFile < files.Count; iFile++)
{
HttpPostedFile postedFile = files[iFile];
if (!string.IsNullOrEmpty(postedFile.FileName))
{
BZ = 1;
string fileName = "";//定义文件名
fileName = Path.GetFileName(postedFile.FileName);//得到上传文件的完整名称 即文件名+后缀名
//int index = fileName.IndexOf(".");
string tempPath = "", dPath = "", ctype = ""; ;
if (files.AllKeys[iFile].IndexOf("FileML") > -1)
{
tempPath = pathML;
dPath = dML;
ctype = "ML";
}
string mypath = tempPath + "/" + fileName;
try
{
postedFile.SaveAs(mypath); //保存附件
//添加附件信息数据
BusinessAttModel uam = new BusinessAttModel();
uam.AttName = fileName.Substring(0, fileName.IndexOf('.'));
uam.ProjectId = int.Parse(pId);
uam.AttPath = dPath + "/" + fileName;
uam.EmployeeID = Session["LoginUser"].ToString();
uam.CHTNo = htNo;
uam.CType = ctype;
PB.BusinessSaveAtt(uam);
R = true;
}
catch (Exception ex)
{
PB.DeleteBusinessAtt(pId, htNo, ctype); //捕获到异常就删除当前附件的记录 遗留附件没关系,在上传可以覆盖
R = false;
break;
}
}
}
if (BZ == 0) //没上传附件
{
R = true;
}
return R;
}