前台代码:
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="filebut" runat="server" Text="上传" onclick="filebut_Click" />
后台代码:
protected void filebut_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == "")
{
Response.Write("<script>alert('请选择上传文件!')</script>");
}
else
{
HttpFileCollection uploadfiles = Request.Files;
string filetiem = DateTime.Now.ToString();//当前时间
string fileusername = Session.Contents["userid"].ToString();//当前用户
string gongwid = Request.QueryString["id"];//文章id
for (int i = 0; i < uploadfiles.Count; i++)
{
HttpPostedFile mypost = uploadfiles[i];
try
{
string filepath = mypost.FileName;
string filename = filepath.Substring(filepath.LastIndexOf("\") + 1);//获取文件名
string serverpath = Server.MapPath("UploadFile\") + filename;//服务器上传地址
if (System.IO.File.Exists(serverpath))
{
Response.Write("<script>alert('文件已存在请重新命名!')</script>");
}
else
{
mypost.SaveAs(serverpath);
string filestrsql = "insert into OA_FILE(wjlj,wjm,gongwid,scr,scsj)values('" + serverpath + "','" + filepath + "','" + gongwid + "','" + fileusername + "','" + filetiem + "')";//sql执行语句
int sfcg = SqlHelper.wjsc(filestrsql);
if (sfcg > 0)
{
labzt.Text = "文件" + i + "上传成功!路径:" + serverpath + "
";
}
}
}
catch (System.Exception ex)
{
labzt.Text = "上传错误!原因:" + ex.Message.ToString();
}
}
}
}
由于先前是准备多加价格fieldupload控件的,但是不是特别美观,但是实现了上传的功能。