每次上传一个文件,可以上传多个文件,支持删除文件,界面不怎么美观,没写样式,后期可以自己加。
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; }
}
}