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; }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值