asp.net——文件上传

每次上传一个文件,可以上传多个文件,支持删除文件,界面不怎么美观,没写样式,后期可以自己加。

aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UplodeFile.aspx.cs" Inherits="Demo.UplodeFile" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div>
                <div>
                    <div>
                        UploadFile
                             <asp:FileUpload ID="FileUpload1" runat="server" />
                        <asp:Button ID="btnUploadFile" runat="server" Text="Upload" OnClick="btnUploadFile_Click" />
                    </div>
                </div>
            </div>
            <div>
                <div>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                        GridLines="None" OnRowDeleting="GridView1_RowDeleting" Width="50%" >
                        <Columns>
                            <asp:TemplateField HeaderText="FileName">
                                <ItemTemplate>
                                    <a target="_blank" href="../Files/<%# Eval("NewFileName")%>">
                                        <%# Eval("FileName")%></a>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Del">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lbtnDel" runat="server" CommandName="Delete">Del</asp:LinkButton>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            No records.
                        </EmptyDataTemplate>
                    </asp:GridView>
                </div>
            </div>
        </div>
    </form>
</body>
</html>

aspx.cs页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo
{
    public partial class UplodeFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

            }
        }

        protected void btnUploadFile_Click(object sender, EventArgs e)
        {
            string newfileName = "";
            string fileName = this.FileUpload1.FileName;

            //1.磁盘存储
            string path = Server.MapPath("~/Files/");

            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            Random ran = new Random();
            if (FileUpload1.HasFile)
            {
                newfileName = fileName.Substring(0, fileName.LastIndexOf('.')) + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileName.Substring(fileName.LastIndexOf('.'));
                this.FileUpload1.PostedFile.SaveAs(path + newfileName);
            }

            //2.数据库存储
            AttachmentModel model = new AttachmentModel();
            model.FileName = fileName;
            model.NewFileName = newfileName;
            model.FilePath = path + newfileName;

            List<AttachmentModel> list = new List<AttachmentModel>();
            if (ViewState["Attachment"] != null)
            {
                list = (List<AttachmentModel>)ViewState["Attachment"];
                model.SeqNo = list.Count + 1;
                list.Add(model);
            }
            else
            {
                model.SeqNo = 1;
                list.Add(model);
            }
            ViewState["Attachment"] = list;

            //3.重新绑定
            BindAttachment();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int SeqNo = int.Parse(GridView1.DataKeys[e.RowIndex][0].ToString());
            List<AttachmentModel> list = (List<AttachmentModel>)ViewState["Attachment"];
            int seqId = 1;
            foreach (AttachmentModel model in list)
            {
                if (model.SeqNo == SeqNo)
                {
                    list.Remove(model);
                    break;
                }
            }
            foreach (AttachmentModel model in list)
            {
                model.SeqNo = seqId;
                seqId++;
            }
            ViewState["Attachment"] = list;

            //3.重新绑定
            BindAttachment();
        }
        public void BindAttachment()
        {
            if (ViewState["Attachment"] != null)
            {
                List<AttachmentModel> list = (List<AttachmentModel>)ViewState["Attachment"];
                string[] a = { "SeqNo" };
                GridView1.DataSource = list;
                GridView1.DataKeyNames = a;
                GridView1.DataBind();
            }
            else
            {
                this.GridView1.DataSource = null;
                this.GridView1.DataBind();
            }
        }
    }
}

AttachmentModel类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Demo
{
    [Serializable]
    public class AttachmentModel
    {
        public string FileName { get; set; }
        public string NewFileName { get; set; }
        public string FilePath { get; set; }
        public int SeqNo { get; set; }
    }
}

逐个模块给出以下说明: 一个用户只能在同一时间登陆一次,需要经过电脑ip验证,用户名和密码验证,全部通过才可以进入首页。 用户登录:首先进入首页,显示该用户今天上传的图片。 1.查看图片:显示该用 户上传的所有图片,显示内容:图像描述,图像大小,是否验证,上传时间,查看,删除 2.上传图片:用户可以上传图片,在上传时有预览的功能,如果数据库中已存在上传过的照片,则提醒用户已上传,否则进入等待验证页面,等待后台验证,若验证成功则显示上传的图像,如果失败则提醒用户是否继续等待验证 3.修改口令:用户可以修改自己的密码 4.退出系统:用户退出当前系统,注销 5.团队简介和客户服务主要是显示团队介绍和联系方式 管理员登陆:进入首页,显示今天上传的所有图片,显示内容:编号,id,图像描述,图像大小,是否验证,上传用户,ip,上传时间,查看,删除 1.查看用户:显示用户名,密码,增加时间,上传几张图片,删除 2.增加用户:包括用户名,密码 3.查找用户:根据输入的用户名进行模糊查询,显示内容:用户名,密码,添加时间,上传几张图片,删除 4.查看所有图片:显示编号,id,图像描述,图像大小,是否验证,上传用户,ip,上传时间,查看,操作,其中id,图像描述,图像大小,是否验证,上传用户,ip,上传时间字段具有排序功能 5.查找图片:可以根据用户名和上传日期进行查询 6.查看节点:显示IP地址,删除 7.增加节点:输入客户端的ip地址进行添加 8.退出系统:进行注销 9.团队简介和客户服务:主要是显示团队介绍和联系方式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值