前台 :
<table >
<tr>
<td >
类型名称:</td>
<td>
<asp:dropdownlist id="typeddl" runat="server" width="100px">
</asp:dropdownlist>
<asp:label id="lb1" runat="server" text="创建人 :"></asp:label>
<asp:label id="writelb" runat="server" text=""></asp:label>
<asp:label id="lb2" runat="server" text="创建时间:"></asp:label>
<asp:label id="createtimelb" runat="server" text=""></asp:label>
</td>
</tr>
<tr>
<td>
文档标题:</td>
<td>
<asp:textbox id="titletb" runat="server" width="300px"></asp:textbox></td>
</tr>
<tr>
<td>
文档内容:</td>
<td>
<asp:textbox id="contenttb" runat="server" textmode="multiline" rows="20" columns="80"></asp:textbox></td>
</tr>
<tr>
<td>
附件上传:</td>
<td>
<input id="findfile" runat="server" type="file" />
<asp:button id="addbt" runat="server" text="上 传" onclick="addbt_click" /></td>
</tr>
<tr>
<td>
</td>
<td>
<asp:datalist id="viewattachdl" runat="server" repeatdirection="horizontal" backcolor="#cccccc" bordercolor="#999999" borderstyle="solid" borderwidth="3px" cellpadding="4" cellspacing="2" forecolor="black" gridlines="both" onitemcommand="viewattachdl_itemcommand">
<itemtemplate>
<table>
<tr>
<td align="left"><asp:label id="attachviewlb" runat="server" text='<%#eval("filename") %>'></asp:label>
</td>
</tr>
<tr>
<td align="center">
<asp:imagebutton id="attachimgbt" commandname="attachimgbt" runat="server" imageurl="~/images/icon-delete.gif" commandargument='<%#eval("fileid") %>'/>
</td>
</tr>
</table>
</itemtemplate>
<footerstyle backcolor="#cccccc" />
<selecteditemstyle backcolor="#000099" font-bold="true" forecolor="white" />
<itemstyle backcolor="white" />
<headerstyle backcolor="black" font-bold="true" forecolor="white" />
</asp:datalist>
<asp:label id="labdetial" runat="server" text=""></asp:label>
<asp:label id="document_attachstatuslb" runat="server" text=""></asp:label>
<asp:label id="filelb" runat="server" text=""></asp:label></td>
</tr>
<tr>
<td>
</td>
<td>
<asp:button id="savebt" runat="server" text="保 存" onclick="savebt_click" />
<asp:button id="returnbt" runat="server" text="返 回" onclick="returnbt_click" />
<asp:label id="labstate" runat="server" text=""></asp:label></td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
后台 :
using system;
using system.io;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class document_add : system.web.ui.page
{
sqldataaccess sqlda = new sqldataaccess();
datatable dt = new datatable();
function fc = new function();
string typeid = "-1";
arraylist files = new arraylist();
static arraylist hif = new arraylist();
int filesuploaded = 0;
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
files.clear();
if (request.params["typeid"] != null)
{
typeid = request.params["typeid"].tostring();
viewstate["typeid"] = typeid;
getrequest();
bindtypeddl(typeid);
}
else
{
this.lb1.visible = false;
this.lb2.visible = false;
this.writelb.visible = false;
this.createtimelb.visible = false;
bindtypeddl();
}
}
}
protected void bindtypeddl()
{
string strcmd = "select * from companydocumenttype";
dt = sqlda.gettable(strcmd);
this.typeddl.datasource = dt;
this.typeddl.datavaluefield = "typeid";
this.typeddl.datatextfield = "typename";
this.typeddl.databind();
}
protected void bindtypeddl(string typeid)
{
string strcmd = "select * from companydocumenttype where typeid=" + typeid;
dt = sqlda.gettable(strcmd);
this.typeddl.datasource = dt;
this.typeddl.datavaluefield = "typeid";
this.typeddl.datatextfield = "typename";
this.typeddl.databind();
}
protected void getrequest()
{
if (viewstate["typeid"] != null)
{
typeid = viewstate["typeid"].tostring();
}
if (typeid != "-1")
{
string strtxt = "select * from companydocumenttype inner join xt_user on companydocumenttype.writeid=xt_user.userid where typeid=" + typeid;
dt = sqlda.gettable(strtxt);
if (dt.rows.count != 0)
{
this.writelb.text = dt.rows[0]["username"].tostring();
this.createtimelb.text = fc.getformattime(dt.rows[0]["createtime"].tostring());
}
//string strcmd = "select * from companydocument inner join companydocumenttype on companydocument.typeid=companydocumenttype.typeid inner join xt_user on xt_user.userid=companydocument.writeid where companydocument.typeid=" + typeid;
//dt = sqlda.gettable(strcmd);
}
}
protected void binddatalist(datatable datalistdt)
{
if (datalistdt.rows.count != 0)
{
this.viewattachdl.datasource = datalistdt;
this.viewattachdl.databind();
this.viewattachdl.visible = true;
}
else
{
this.viewattachdl.visible = false;
}
}
protected void addbt_click(object sender, eventargs e)
{
string path = "../company/空类型/", typename = "空类型";
this.filelb.text = "";
document_attachstatuslb.text = "";
this.labdetial.text = "";
if (viewstate["path_type_file"] == null)
{
string strcmd = "select * from companydocumenttype where typeid=" + this.typeddl.selectedvalue;
dt = sqlda.gettable(strcmd);
if (dt.rows.count != 0)
{
typename = dt.rows[0]["typename"].tostring();
}
directoryinfo dirinfo = new directoryinfo(server.mappath("../company/" + typename + "/"));
path = server.mappath("../company/" + typename + "/");
viewstate["path"] = path;
if (!dirinfo.exists)
{
directory.createdirectory(server.mappath("../company/" + typename + "/"));
}
}
//检查上传路径是否已经包含此文件
bool saveas = true;
bool fileok = false;
string fn = system.io.path.getfilename(this.findfile.postedfile.filename);
string[] str = directory.getfiles(viewstate["path"].tostring());
string fileexception = system.io.path.getextension(fn).tolower();
string[] allowedexception ={ ".doc", ".txt" };
if (str.length != 0)
{//目录下有文件时做出比较
for (int j = 0; j < str.length; j++)
{
if (system.io.path.getfilename(str[j]) == system.io.path.getfilename(findfile.postedfile.filename))
{
saveas = false;
}
}
if (saveas)
{
for (int i = 0; i < hif.count; i++)
{//检查数组当中是否已经存在此文件
htmlinputfile hif = hif[i] as htmlinputfile;
if (system.io.path.getfilename(hif.value) == system.io.path.getfilename(findfile.postedfile.filename))
{
saveas = false;
}
}
if (saveas)
{
for (int j = 0; j < allowedexception.length; j++)
{
if (fileexception == allowedexception[j])
fileok = true;
}
if (fileok)
{
hif.add(findfile);
this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + " 上传成功!";
}
else
{
this.filelb.text = "请上传.doc文件,或.txt文件!";
}
}
else
{
this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + " 文件已经上传,请不要重复上传!";
}
}
else
{
this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + " 文件已经存在,请检查company下的文件!";
}
}
else
{//目录下无文件直接保存!
for (int i = 0; i < hif.count; i++)
{//检查数组当中是否已经存在此文件
htmlinputfile hif = hif[i] as htmlinputfile;
if (hif.count >= 1)
{
if (system.io.path.getfilename(hif.value) == system.io.path.getfilename(findfile.postedfile.filename))
{
saveas = false;
}
}
else
{
saveas = true;
}
}
if (saveas)
{
for (int j = 0; j < allowedexception.length; j++)
{
if (fileexception == allowedexception[j])
fileok = true;
}
if (fileok)
{
hif.add(findfile);
this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + " 上传成功!";
}
else
{
this.filelb.text = "请上传.doc文件,或.txt文件!";
}
}
else
{
this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + " 文件已经上传,请不要重复上传!";
}
}
binddatalist(gettable());
}
protected void returnbt_click(object sender, eventargs e)
{
response.redirect("~/documentmanage/document_view.aspx?tag=" + this.typeddl.selectedvalue);
}
protected datatable gettable()
{
datatable dt=new datatable();
datatable table = new datatable();
table.columns.add("filename");
table.columns.add("fileid");
hif.trimtosize();
int i = 0;
foreach(system.web.ui.htmlcontrols.htmlinputfile hif in hif)
{
datarow row =table.newrow();
string fn = system.io.path.getfilename(hif.postedfile.filename);
row["filename"] = fn;
row["fileid"] = i++;
table.rows.add(row);
}
dt = table;
return dt;
}
protected void savebt_click(object sender, eventargs e)
{
string documentid = "-1";
if (this.titletb.text != "")
{
if (viewstate["path"] != null)
{//执行代有附件的保存
string baselocation = viewstate["path"].tostring();
string status = "";
int document_attachstatus = -1;
int status = -1;
this.filelb.text = "";
document_attachstatuslb.text = "";
this.labdetial.text = "";
int i = 0;
foreach (system.web.ui.htmlcontrols.htmlinputfile hif in hif)
{
try
{
i++;
string fn = system.io.path.getfilename(hif.postedfile.filename);
if (documentid == "-1")
{//生成companydocument内的唯一documentid
documentid = datetime.now.date.tostring("yyyymmdd") + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.millisecond.tostring();
}
if (i == 1)
{//多文件上传只插入一个documentid
if (session["userid"] != null)
{
string strcmd1 = "insert into companydocument(documentid,writeid,typeid,tilte,content) values('"
+ documentid + "','" + session["userid"].tostring() + "','" + this.typeddl.selectedvalue + "','" + this.titletb.text
+ "','" + this.contenttb.text + "')";
status = sqlda.executesql(strcmd1);
}
else
{
response.write("<script>alert('提示:重新登陆!')</script>");
}
}
string txt = "select * from document_attach";
dt = sqlda.gettable(txt);
if (dt.rows.count != 0)
{//document_attach表内有数据进行判断
bool insertattach = true;
for (int j = 0; j < dt.rows.count; j++)
{
if (dt.rows[j]["url"].tostring() == hif.postedfile.filename.replace(@"//", @"/"))
{
insertattach = false;
}
}
if (insertattach)
{
string strtxt = "insert into document_attach(documentid,url) values('"
+ documentid + "','" + baselocation + fn + "')";
document_attachstatus = sqlda.executesql(strtxt);
}
else
{
document_attachstatuslb.text = "数据库当中已经存在此文件,请重新命名!";
}
}
else
{//document_attach表内无数据,直接插入
&nb, sp; string strtxt = "insert into document_attach(documentid,url) values('"
+ documentid + "','" + baselocation + fn + "')";
document_attachstatus = sqlda.executesql(strtxt);
}
if (document_attachstatus > 0)
{
hif.postedfile.saveas(baselocation + fn);
filesuploaded++;
status += fn + "<br>";
labdetial.text = status + " 文件上传成功:<br>";
}
}
catch (exception err)
{
labdetial.text = "错误 文件保存 " + baselocation
+ "<br>" + err.tostring();
}
}
}
else
{//执行无附件的保存
if (documentid == "-1")
{//生成companydocument内的唯一documentid
documentid = datetime.now.date.tostring("yyyymmdd") + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.millisecond.tostring();
}
if (session["userid"] != null)
{
string strcmd1 = "insert into companydocument(documentid,writeid,typeid,tilte,content) values('"
+ documentid + "','" + session["userid"].tostring() + "','" + this.typeddl.selectedvalue + "','" + this.titletb.text
+ "','" + this.contenttb.text + "')";
int status = sqlda.executesql(strcmd1);
}
else
{
response.write("<script>alert('提示:重新登陆!')</script>");
}
}
hif.clear();
this.titletb.text = "";
this.contenttb.text = "";
this.labdetial.text = "保存成功!";
binddatalist(gettable());
}
else
{//this.titletb.text == ""
labdetial.text = "提示:请填写文档标题";
binddatalist(gettable());
return;
}
}
protected void viewattachdl_itemcommand(object source, datalistcommandeventargs e)
{
if (e.commandname == "attachimgbt")
{
int fileid =int32.parse(e.commandargument.tostring());
hif.removeat(fileid);
binddatalist(gettable());
this.filelb.text = "";
document_attachstatuslb.text = "";
this.labdetial.text = "";
}
}
}//end_class
新闻热点
疑难解答
图片精选