C# 本地文件的上传和下载

本文主要介绍一下,在APS.NET中文件的简单上传于下载,上传是将文件上传到服务器的指定目录下,下载是从存入数据库中的路径,从服务器上下载。

  1.上传文件

  (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>
复制代码

  (2).后台代码  

复制代码
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.下载文件

  (1)前台页面

复制代码
<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>
复制代码

  (2)后台代码

  1).绑定数据

复制代码
        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();

        }
复制代码

  2).下载文件

  

复制代码
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();
            }
        }
复制代码

  以上就完成了,简单的文件上传与下载。 

posted @ 2017-07-21 14:41  爱生活,爱代码  阅读(1033)  评论(0编辑  收藏  举报