第1个回答 推荐于2016-08-14
#region 文件上传
string path = null;
string name = null;
string type2 = null;
string upfile = null;
if (fu.HasFile)
{
try
{
name = fu.FileName;
type2 = name.Substring(name.LastIndexOf(".") + 1);
upfile = System.DateTime.Now.ToString("yyyyMMddhhmmss") + "." + type2;
if (type2.ToLower() == "rar" || type2.ToLower() == "zip" || type2.ToLower() == "doc" || type2.ToLower() == "xls" || type2.ToLower() == "ppt")
{
path = Server.MapPath("file") + "\\";
if (!File.Exists(path))
{
Directory.CreateDirectory(path);
fu.SaveAs(path + name);
}
fu.SaveAs(path + name);
}
else
{
Response.Write("<script>alert('格式不正确(格式(.doc,.xls,.rar,.zip,.ppt)!');</script>");
Response.End();
}
}
catch(Exception ex)
{
Response.Write("<script>alert('上传格式错误:" + ex.Message.ToString() + "');window.location.href='Upload.aspx';</script>");
}
}
else
{
upfile = "N/A";
name="N/A";
}
if (upfile != "N/A" || name!="N/A")
{
UpLoad up = new UpLoad();
UpLoadBll uBll = new UpLoadBll();
up.setFileName(upfile);
up.setU_name(name);
if (uBll.insertFile(up))
{
Response.Write("<script>alert('" + uBll.getMsg() + "');window.location.href='Upload.aspx';</script>");
}
else
{
Response.Write("<script>alert('" + uBll.getMsg() + "');window.location.href='Upload.aspx';</script>");
}
}
else
{
Response.Write("<script>alert('上传文件的内容或上传文件不能为空!');window.location.href='Upload.aspx';</script>");
}
}
#endregion
然后把upfile 插入到数据库就OK了本回答被提问者和网友采纳