ASP.NET网络硬盘(文件上传,文件下载)

文件上传:
界面:
前台代码:
<body style="text-aligncenterbackground-imageurl(Images/bg6.bmp);">
    <form id="form" method="post" runat="server" enctype="multipart/form-data">
        <p style="text-aligncenter">
             
        </p>
        <div>
            <div style="text-aligncenter">
                <br />
                <table border="0" cellpadding="0" cellspacing="0" style="background-imageurl(Images/wjsc.bmp)width412pxheight293px">
                    <tr>
                        <td style="width105pxtext-aligncenter">
                            <div style="text-aligncenter">
                                <div style="text-aligncenter">
                                    <table cellpadding="0" cellspacing="0" style="width410pxheight177pxtext-aligncenter;">
                                        <tr>
                                            <td align="left" style="width100pxheight42pxtext-alignleft"></td>
                                        </tr>
                                        <tr>
                                            <td align="left" style="width100pxheight30pxtext-aligncenter"></td>
                                        </tr>
                                        <tr>
                                            <td align="left" style="width100pxheight37pxtext-aligncenter"> 
                                                    <input type="file" name="file" style="width370px" id="File1" language="javascript" onclick="return File1_onclick()" runat="server" /></td>
                                        </tr>
                                        <tr>
                                            <td align="left" style="width100pxheight30pxtext-aligncenter"> 
                                        <asp:Label ID="lbltishi" runat="server" Font-Bold="False" Font-Size="9pt" ForeColor="Red"
                                            Height="20px" Width="355px" Text="提示:请您选择要上传的文件"></asp:Label></td>
                                        </tr>
                                        <tr>
                                            <td style="width100pxheight30pxtext-aligncenter;">
                                                <asp:Button runat="server" Text="上传文件" ID="BtnUp" OnClick="BtnUp_Click" Width="128px" SkinID="btnSkin"></asp:Button></td>
                                        </tr>
                                        <tr>
                                            <td style="width100pxheight21px"> </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
                 
            </div>
        </div>
    </form>
 
</body>
后台代码:
    //上传方法
    public void Up(int id)
    {
        HttpFileCollection UpLoad = HttpContext.Current.Request.Files; //获取页面所有上传文件
        //实现多个文件上传,逐个获取
        for (int i = 0; i < UpLoad.Count; i++)
        {
            HttpPostedFile file = UpLoad[i];//HttpPostedFile 类提供用于获取关于单独的文件的信息和读取及保存文件的属性和方法。
            string fileName = Path.GetFileName(file.FileName);//获取页面上传文件的文件名
            if (fileName != null)
            {
                file.SaveAs(MapPath("mr/") + fileName);//保存上传内容
                directory.Insert(fileName, id, file.ContentLength, "mr/" + fileName, file.ContentType);//插入数据方法
 
            }
        }
        Response.Write("<script>alert('恭喜您!文件上传成功!');window.close();</script>");
    }
    //上传按钮
    protected void BtnUp_Click(object sender, EventArgs e)
    {
        if (this.File1.PostedFile.FileName == "")
        {
            Response.Write("<script>alert('很遗憾,上传文件不能为空!')</script>");
 
        }
        else
        {
            Up(ID1);
 
        }
    }
    //插入数据(directory.Insert)
    public int Insert(string mrming, int id, int nsize, string path, string s)
    {
        SqlConnection con = new SqlConnection(GetConStr());
        SqlCommand cmd = new SqlCommand("procInsert", con);
        cmd.CommandType = CommandType.StoredProcedure;
        ///添加存储过程的参数
        SqlParameter pmrming = new SqlParameter("@mrming"SqlDbType.VarChar, 200);
        pmrming.Value = mrming;
        cmd.Parameters.Add(pmrming);
        SqlParameter pmrid = new SqlParameter("@mrid"SqlDbType.Int, 4);
        pmrid.Value = id;
        cmd.Parameters.Add(pmrid);
        SqlParameter psize = new SqlParameter("@size"SqlDbType.Int, 4);
        psize.Value = nsize;
        cmd.Parameters.Add(psize);
        SqlParameter path1 = new SqlParameter("@path"SqlDbType.VarChar, 255);
        path1.Value = path;
        cmd.Parameters.Add(path1);
        SqlParameter leibie1 = new SqlParameter("@Leibie"SqlDbType.VarChar, 200);
        leibie1.Value = s;
        cmd.Parameters.Add(leibie1);
        int i = -1;
        con.Open();
        i = cmd.ExecuteNonQuery();
        con.Close();
        return i;
    }
 
下载模块:
    protected void rpZiYuan_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "downLoad")                      //下载
        {
            try
            {
                sql = "update T_ZiYuan set F_DownloadTimes =F_DownloadTimes+1 where F_Id=" + e.CommandArgument;
                cs.ExecuteUpdate(sql);  //更新下载次数
                
                sql = "select * from T_ZiYuan where F_Id=" + e.CommandArgument;
                DataRow row = cs.GetDataSet(sql).Tables[0].Rows[0];
                string filePath = row["F_ZiYuanPath"].ToString();
 
                filePath = Server.MapPath("~/ZiYuan/" + filePath);
 
                FileStream fs = new FileStream(filePath, FileMode.Open); //建立文件流
                long fileSize = fs.Length;
 
                Context.Response.ContentType = "application/octet-stream"//设置输出流的 HTTP MIME 类型为application/octet-stream
                Context.Response.AddHeader("Content-Disposition""attachment;fileName="" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8) + """);//将 HTTP 头添加到输出流
                Context.Response.AddHeader("Content-Length", fileSize.ToString());//增加报文头
 
                byte[] fileBuffer = new byte[fileSize];
                fs.Read(fileBuffer, 0, (int)fileSize);
                fs.Close();
                Context.Response.BinaryWrite(fileBuffer);
                Context.Response.End();
                //Response.Redirect("DownLoad.aspx");
 
               
 
            }
            catch
            {
                throw new Exception("对不起,文件下载错误!");
            }
        }
    }
原文地址:https://www.cnblogs.com/zt102545/p/3321233.html