1、aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Upload.aspx.cs" Inherits="ZhanJiang.CatalogManage.Upload" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>文档上传</title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td> <asp:Button runat="server" Text="上传" ID="UploadButton" OnClick="Upload_Click" /> </td> <td> <asp:Button runat="server" Text="取消" ID="CancelButton" onclientclick="window.close();"/> </td> </tr> </table> </div> <div> <table style="height: 10px"> <tr> </tr> </table> </div> <div> <table> <tr> <td> 月份: </td> <td style="width: 100px"> <asp:DropDownList runat="server" ID="ddlGKType"> <asp:ListItem Value="">-请选择-</asp:ListItem> <asp:ListItem Value="1">1</asp:ListItem> <asp:ListItem Value="2">2</asp:ListItem> <asp:ListItem Value="3">3</asp:ListItem> <asp:ListItem Value="4">4</asp:ListItem> <asp:ListItem Value="5">5</asp:ListItem> <asp:ListItem Value="6">6</asp:ListItem> <asp:ListItem Value="7">7</asp:ListItem> <asp:ListItem Value="8">8</asp:ListItem> <asp:ListItem Value="9">9</asp:ListItem> <asp:ListItem Value="10">10</asp:ListItem> <asp:ListItem Value="11">11</asp:ListItem> <asp:ListItem Value="12">12</asp:ListItem> </asp:DropDownList> </td> <td> 上传文件名: </td> <td> <input id="InputFile" type="file" runat="server" /> </td> <tr> <tr> <td> <asp:Label ID="Label1" runat="server" Text="" ></asp:Label> </td> </tr> </table> </div> </form> </body> </html>
2、cs文件:
using System; using System.Collections; using System.Configuration; using System.Data; 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; using System.Data.SqlClient; namespace Upload { public partial class Upload : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //默认月份下拉框为当前月 DateTime to = DateTime.Now; ddlGKType.SelectedValue = to.Month.ToString(); } /// <summary> /// 检测指定的文件夹是否存在,不存在就创建 /// </summary> /// <param name="docpath">该文件夹的之前的路径,注意一定要带上"/"</param> /// <returns></returns> public string CheckFile(string docpath) { //命名一个年度的文件夹 string folder = DateTime.Now.Year.ToString(); //判断文件是否存在 if (!System.IO.Directory.Exists("新建文件夹名/" + folder)) { //自动生成文件夹 System.IO.Directory.CreateDirectory("新建文件夹名/" + folder); } //生成后返回文件夹名 return "新建文件夹名/" + folder; } /// <summary> /// 上传文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Upload_Click(object sender, System.EventArgs e) { try { string sqlStr = ConfigurationManager.ConnectionStrings["连接字符串"].ToString(); using (SqlConnection sqlcon = new SqlConnection(sqlStr)) { string uploadName = InputFile.Value;//获取待上传的完整路径,包括文件名 //string uploadName = InputFile.PostedFile.FileName; System.IO.FileInfo fi = new System.IO.FileInfo(uploadName); string upTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//上传时间 int upYear = Convert.ToInt32(DateTime.Now.Year.ToString());//获取年度 string upMonth = ddlGKType.SelectedValue.ToString();//获取上传月份 string name = fi.Name;//获取名称 string type = fi.Extension;//获取类型 if (type == ".txt" || type == ".doc" || type == ".docx" || type == ".xlsx" || type == ".pdf") { string ID = Guid.NewGuid().ToString(); string docName = "";//上传后的文件名 if (InputFile.Value != "") { int idx = uploadName.LastIndexOf("."); string suffix = uploadName.Substring(idx);//获得上传后缀名 docName = ID + name;//以ID加上传文件名作为保存文件名,防止重复 } if (uploadName != "") { //string path = Server.MapPath("~\\Doc");//保存到文件夹下 string path1 = "新建文件夹名/"; path1 = CheckFile(path1) + "/" + docName;//完整路径 InputFile.PostedFile.SaveAs(Server.MapPath("~/" + path1));//保存路径 string sql = "insert into 表名(字段1 , 字段2 , 字段3 , 字段4 , 字段5) values('" + ID + "','" + upYear + "','" + upMonth + "','" + name + "','" + upTime + "')"; SqlCommand cmd = new SqlCommand(sql, sqlcon); sqlcon.Open(); cmd.ExecuteNonQuery(); this.Label1.Text = "上传成功!"; } } else { this.Label1.Text = "请上传正确的文件格式"; } } } catch (Exception ex) { Response.Write(ex); } } } }