ASP.NET文件的上传与下载
(1)上传文件前台:
<table align="center" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="right">文件名称:</td>
<td>
<asp:TextBox ID="FileName" runat="server"></asp:TextBox>
</td>
<td></td>
</tr>
<tr>
<td align="right">浏览:</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="UploadButton" runat="server" Text="上传"
onclick="UploadButton_Click" /><asp:Label ID="Msg" runat="server" ForeColor="#FF0000"></asp:Label>
</td>
<td></td>
</tr>
</table>
上传文件后台:
protected void UploadButton_Click(object sender, EventArgs e)
{
Msg.Text = "";
MyWebSite.DAL.FileInfo file = new MyWebSite.DAL.FileInfo();
if (string.IsNullOrEmpty(FileName.Text))
{
Msg.Text = "请输入文件名";
return;
}
if (FileUpload1.HasFile)
{
try
{
DateTime upload = DateTime.Now;
string flag = Path.GetExtension(FileUpload1.PostedFile.FileName);
string path = Server.MapPath("../Upload/" + FileUpload1.FileName);
string size = (FileUpload1.PostedFile.ContentLength / 1024).ToString();
FileUpload1.SaveAs(path);
file.FileName = FileName.Text.ToString();
file.Type = flag;//获得文件格式
file.Size = size;//文件大小
file.FilePath = path;//上传到服务器的绝对路径
file.UploadTime = DateTime.Now.ToShortDateString();//上传时间
handler.AddFile(file);//将上传的文件信息保存到数据库
Msg.Text = "上传成功!";
}
catch
{
Msg.Text = "上传失败!";
}
}
}
(2)下载文件前台:
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="down_file_Click">
<ItemTemplate>
<table width="90%" align="center" border="1" cellpadding="1" cellspacing="0" bgcolor="#e1e1e1" class="title_font">
<tr>
<td class="title_font" width="10%" align="center">文件名称:</td>
<td width="20%"align="center"><b><asp:Label ID="FileTitle" runat="server" Text='<%#Eval("文件名称") %>'></asp:Label></b></td>
<td width="6%"align="center">类型:</td>
<td width="6%"align="center"><%#Eval("类型") %></td>
<td width="10%" align="center">文件大小:</td>
<td width="8%"align="center"><%#Eval("文件大小") %>KB</td>
<td width="10%"align="center">上传时间:</td>
<td ><%#Eval("上传时间") %></td>
<td width="10%" colspan="2" align="center"><asp:LinkButton ID="LinkButton1" CommandArgument='<%#Eval("link") %>' runat="server">下载文件</asp:LinkButton>
</td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:Repeater>
下载文件后台(绑定数据)
public void GrvDataBin(List<FileInfo> list)
{
DataView dv = new DataView();
DataTable dt = new DataTable("fileMeta");
{
dt.Columns.Add("文件名称");
dt.Columns.Add("类型");
dt.Columns.Add("上传时间");
dt.Columns.Add("文件大小");
dt.Columns.Add("link");
}
foreach (FileInfo fileM in list)
{
DataRow row = dt.NewRow();
row[0] = fileM.FileName;
row[1] = fileM.Type;
row[2] = fileM.UploadTime;
row[3] = fileM.Size;
row[4] = fileM.FilePath;
dt.Rows.Add(row);
}
dv.Table = dt;
Repeater1.DataSource = dv;
Repeater1.DataBind();
}
下载文件后台(下载)
protected void down_file_Click(object sender, RepeaterCommandEventArgs e)
{
System.IO.FileStream fs = null;
try
{
string filePath = e.CommandArgument.ToString();
string fileName = ((Label)e.Item.FindControl("FileTitle")).Text.ToString();
fs = System.IO.File.OpenRead(filePath);
byte[] buffer = new byte[1024];
long count = 1024;
Response.Buffer = true;
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(System.IO.Path.GetFileName(filePath)));//下载时要保存的默认文件名
Response.AddHeader("Content-Length", fs.Length.ToString());
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
Response.BinaryWrite(buffer);
}
}
catch (Exception ex)
{
return;
}
finally
{
fs.Close();
}
}
参考文章:http://blog.ncmem.com/wordpress/2023/11/09/asp-net%e6%96%87%e4%bb%b6%e7%9a%84%e4%b8%8a%e4%bc%a0%e4%b8%8e%e4%b8%8b%e8%bd%bd/
欢迎入群一起讨论