1、aspx页面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <%@ 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文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 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); } } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步