62,041
社区成员
发帖
与我相关
我的任务
分享
<asp:TextBox ID="txtPlandate" runat="server" class="txtbox" onfocus="new WdatePicker()" CssClass=""></asp:TextBox>
<script id="editor" name="editor" type="text/plain" style="width: 800px; height: 300px;"> </script>
<td class="td-text" colspan="5">
<label id="lbInfoPath" for="InfoPath" style="color: blue; height: 100px; width: 100px;"> <%--添加附件--%>
<img alt="" src="../FrameWork/Images/tianjiafujian.png" /> <input style="position: absolute; right: 0; top: 0; font-size: 20px; opacity: 0; filter: alpha(opacity=0);" type="file" name ="InfoPath" id="InfoPath" onchange="ChangeFile();"/> <input type="hidden" value="<%=pid%>" id="hidPid"/> <input type="hidden" value="<%=CreateUser%>" id="hidCreatUsers"/> </label> <br /> <div id="fileshow" runat="server" style="border:1px solid ,red;width:800px;"></div> <br /> </td>
/// 读取文件 public void BindFile(string pid)
{ StringBuilder strshow = new StringBuilder();
HR_OnlineBLL FileDal = new HR_OnlineBLL(); dt.Clear(); dt = FileDal.GetFile(pid);
strshow.Remove(0, strshow.Length);//清空strshow
strshow.Append("<table style='width:100% !important;'><thead><tr><th style='width: 10%;BACKGROUND: #eeeeee'>编号</th><th style='width: 40%;BACKGROUND: #eeeeee'>文件</th><th style='width: 10%;BACKGROUND: #eeeeee'>类型 </th> <th style='width: 10%;BACKGROUND: #eeeeee'>操作 </th></tr> </thead>");
for (int i = 0; i < dt.Rows.Count; i++)
{ string ID = dt.Rows[i]["rowid"].ToString(); FileID = dt.Rows[i]["ID"].ToString(); string filename = dt.Rows[i]["filename"].ToString(); string AttackType = dt.Rows[i]["AttackType"].ToString(); strshow.Append(" <tr><td >" + ID + "</td><td>" + filename + "</td> <td>" + AttackType + "</td> <td>" ); strshow.Append("<input type='image' name='infopath' src='../framework/images/document-delete.png' style='height:20px; width:20px;' onclick='FileSelect(" + pid + "," + FileID + ", 0)'/> " + "</td></tr>");
}
strshow.Append(" </table>"); this.fileshow.InnerHtml = ""; this.fileshow.InnerHtml = strshow.ToString();
}
function ChangeFile() {
if ($("#txtCRSTitle").val() == "") { alert("请输入学习科目"); return;
}
var pid = $("input#hidPid").val(); var users = $("input#hidCreatUsers").val(); FileSelect(pid, 0, users);
}
//选择附件后显示附件名
function FileSelect(pid, FileID, CreateUser) {clickme(6);
$("#form1").ajaxSubmit({
type: 'post',
url: "/HandlerFile/UpdateFileHandler.ashx",
data: { "pid": pid, "FileID": FileID, "CreateUser": CreateUser },
dataType: "json",
async: false,
success: function (result) {
if (result.rkey) { $("div#fileshow").html(""); $("div#fileshow").html(result.rval); clickhide(); var file = $("#InfoPath") file.after(file.clone().val("")); file.remove(); } else {alert(result.rval); } } });
}
namespace ManageCom.HandlerFile
{public class UpdateFileHandler : IHttpHandler
{ string pid = ""; string FileID = ""; string CreateUser = ""; bool msg = false; string fileName = "";//文件名称
string fex = ""; string FileType = "";string str = ""; bool result = false; StringBuilder strshow = new StringBuilder();
HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; public static readonly string FileSize = System.Configuration.ConfigurationManager.ConnectionStrings["FileSize"].ConnectionString;
public static readonly string FilePath = System.Configuration.ConfigurationManager.ConnectionStrings["FilePath"].ConnectionString;
#region MyRegion public void ProcessRequest(HttpContext context)
{ try { pid = context.Request.Form["pid"]; FileID = context.Request.Form["FileID"]; CreateUser = context.Request.Form["CreateUser"];
if (CreateUser != "0" && CreateUser != null) //CreateUser!= "0"/执行添加文件操作
{ result = Filevalidation(hfc); if (result) { if (AddFile(pid))
{ BindFile(pid); msg = true; str = "{\"rkey\":\"" + msg + "\",\"rval\":\"" + strshow.ToString() + "\"}"; }
else
{ msg = false; str = "{\"rkey\":\"" + msg + "\",\"rval\":\"上传失败\"}";
} } else
{ msg = false;
str = "{\"rkey\":\"" + msg + "\",\"rval\":\"上传失败\"}"; }
} else//执行删除操作
{ var DelIsTrue = DelFileByid(FileID); msg = DelIsTrue == true ? DelIsTrue : false;
BindFile(pid); msg = true; str = "{\"rkey\":\"" + msg + "\",\"rval\":\"" + strshow.ToString() + "\"}";
} context.Response.Write(str);
} catch (Exception ex)
{ str = "{\"rkey\":\"false\",\"rval\":\"上传异常,请联系系统管理员\"}"; context.Response.Write(ex.ToString());
}
}
#endregion
public bool Filevalidation(HttpFileCollection hfc)
{
fileName = hfc[0].FileName;
fex = Path.GetExtension(fileName);
if (fileName == "" || fileName == null)
{ return false;
}
else if (hfc[0].ContentLength > Convert.ToInt32(FileSize))
{
return false; }
else if (fex != ".FLV" && fex.ToUpper() != ".AVI" && fex.ToUpper() != ".MOV" && fex.ToUpper() != ".MP4" &&
fex.ToUpper() != ".PDF" && fex.ToUpper() != ".XLS" && fex.ToUpper() != ".XLSX" && fex.ToUpper() != ".DOC" && fex.ToUpper() != ".DOCX" && fex.ToUpper() != ".PPT" && fex.ToUpper() != ".GIF" && fex.ToUpper() != ".JPG" && fex.ToUpper() != ".JPEG" && fex.ToUpper() != ".BMP" && fex.ToUpper() != ".PNG" && fex.ToUpper() != ".TXT")
{
return false;
}
return true;
}
public bool AddFile(string spid)
{
string Path = "../OnlineLearningFile/";
//FileType '0':图片,'1':文件,'2':视频
if (fex == ".FLV" || fex.ToUpper() == ".AVI" || fex.ToUpper() == ".MOV" || fex.ToUpper() == ".MP4")
{
FileType = "2";
Path = "../OnlineLearningFile/video/";
}
else if (fex.ToUpper() == ".PDF" || fex.ToUpper() == ".XLS" || fex.ToUpper() == ".XLSX" || fex.ToUpper() == ".DOC" || fex.ToUpper() == ".DOCX" || fex.ToUpper() == ".PPT" || fex.ToUpper() == ".PPTX" || fex.ToUpper() == ".TXT")
{
FileType = "1";
Path = "../OnlineLearningFile/document/";
}
else if (fex.ToUpper() == ".GIF" || fex.ToUpper() == ".JPG" || fex.ToUpper() == ".JPEG" || fex.ToUpper() == ".BMP" || fex.ToUpper() == ".PNG")
{
FileType = "0";
Path = "../OnlineLearningFile/images/";
}
fileName = Path + hfc[0].FileName;
string NewName = pid + "_" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + fex;//
string DBSavepath = Path + NewName;
string PhysicalPath = System.Web.HttpContext.Current.Server.MapPath(Path + NewName);
hfc[0].SaveAs(PhysicalPath);
//保存到数据库中
result = new HR_OnlineBLL().AddFile(spid, FileType, hfc[0].FileName, DBSavepath, fex, CreateUser);
return result;
}
public StringBuilder BindFile(string spid)
{
HR_OnlineBLL FileDal = new HR_OnlineBLL();
DataTable dt = FileDal.GetFile(spid);
strshow.Remove(0, strshow.Length);
strshow.Append("<table style='width:100% !important;'><thead><tr><th style='width: 10%;BACKGROUND: #eeeeee'>编号</th><th style='width: 40%;BACKGROUND: #eeeeee'>文件</th><th style='width: 10%;BACKGROUND: #eeeeee'>类型 </th> <th style='width: 10%;BACKGROUND: #eeeeee'>操作 </th></tr> </thead>");
for (int i = 0; i < dt.Rows.Count; i++)
{
string ID = dt.Rows[i]["rowid"].ToString();
FileID = dt.Rows[i]["ID"].ToString();
string filename = dt.Rows[i]["filename"].ToString();
string AttackType = dt.Rows[i]["AttackType"].ToString();
strshow.Append(" <tr><td >" + ID + "</td><td>" + filename + "</td> <td>" + AttackType + "</td> <td>");
strshow.Append("<input type='image' name='infopath' src='../framework/images/document-delete.png' style='height:20px; width:20px;' onclick='fileselect(" + pid + "," + FileID + ", 0)'/> " + "</td></tr>");
}
strshow.Append(" </table>");
return strshow;
}
public bool DelFileByid(string sFileID)
{
result = new HR_OnlineBLL().DelFile(sFileID);
return result;
}