ASP.NET FileUpload以及文件下载和Repeater控件显示

来源:互联网 发布:虾米音乐 2.0mac 编辑:程序博客网 时间:2024/05/22 03:04

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="文件夹上传以及Repeater.WebForm1" %><!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>            <asp:FileUpload ID="FileUpload1" runat="server" />            <asp:Button runat="server" ID="btnUpload" Text="上传" OnClick="btnUpload_Click" />        </div>        <div>            文件下载:<br />            <asp:Repeater runat="server" ID="repeater1" >                <HeaderTemplate>                    <table border="1">                        <tr>                            <td>文件顺序</td><td>文件下载</td>                        </tr>                </HeaderTemplate>                <ItemTemplate>                    <tr>                        <td><%# Container.ItemIndex+1 %></td>                        <td><asp:LinkButton runat="server" ID="linkbtnDownloda" Text='<%# Eval("filenames") %>'  CommandArgument='<%#Eval("filepath") %>' OnCommand="linkbtnDownloda_Command"/> </td>                    </tr>                </ItemTemplate>                <FooterTemplate>                    </table>                </FooterTemplate>            </asp:Repeater>        </div>    </form></body></html>
WebForm1.aspx.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;using Model;using BLL;namespace 文件夹上传以及Repeater{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                Bind();            }        }        public void Bind()        {            this.repeater1.DataSource = BLL.FileManager.selectAllFile().Tables[0];            this.repeater1.DataBind();        }        ///        protected void btnUpload_Click(object sender, EventArgs e)        {            string fullfilename=this.FileUpload1.PostedFile.FileName;            string filename = fullfilename.Substring(fullfilename.LastIndexOf("\\") + 1);            string filetype = fullfilename.Substring(fullfilename.LastIndexOf(".") + 1);            Response.Write("文件名:"+filename+",类型:"+filetype);            //////            string savepath = Server.MapPath("~\\Files\\" + filename);            this.FileUpload1.SaveAs(savepath);            //            Model.FileInfo fi = new Model.FileInfo();            fi.Filepath = "~\\Files\\"+filename;            fi.Filenames = filename;            BLL.FileManager.uploadFile(fi);            Bind();        }        protected void linkbtnDownloda_Command(object sender, CommandEventArgs e)        {            string uri = e.CommandArgument.ToString();            string filename = uri.Substring(uri.LastIndexOf("\\") + 1);            //Response.Write(uri);            FileStream fs = new FileStream(Server.MapPath(uri), FileMode.Open);            byte[] bytes = new byte[(int)fs.Length];            fs.Read(bytes, 0, (int)fs.Length);            //用完记得关闭字符流操作            fs.Close();            //设置当前字符流在HTTP上输出的格式为stream字符流格式,固定写法            Response.ContentType = "application/octet-stream";            //通知浏览器进行下载操作,并保存当前下载的文件名到本地            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));            //Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);            //将HTTP中的字符流做写入缓冲操作            Response.BinaryWrite(bytes);            //将当前HTTP缓冲中保存的字符流写入到本地            Response.Flush();            //写入完成后结束整个操作            Response.End();        }    }}



阅读全文
0 0
原创粉丝点击