• ASP.NET 文件上传于下载


      本文主要介绍一下,在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();
                }
            }

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

     
  • 相关阅读:
    Git 几个常用操作
    Ubuntu16.04安装YouCompleteMe
    常用命令总结
    启动Kernel提示Bad Data CRC
    linux4.15.1编译init/mounts报错
    编译Linux-4.15.1内核时遇到:“error : openssl/bio.h :No such file or folder”
    添加mtdparts引起的问题
    arm-linux-ld:u-boot.lds:1: ignoring invalid character `#' in expression
    smartgit的安装
    ubuntu下安装wine
  • 原文地址:https://www.cnblogs.com/akwwl/p/3184239.html
Copyright © 2020-2023  润新知