[Files]多文件上传和下载

最近在做一个信息发布系统。要有上传文件和下载的功能

直接上源码:

   <script language="JavaScript" type="text/javascript" >
  function addFile()
  {
      var str = '<BR><INPUT type="file" size="50" NAME="File" runat="server">'
      document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
  }

 

 table align="center" class="fullwidth">
            <tr>
                <td style="width: 108px; height: 15px">
                    附件:</td>
                <td style="width: 448px; height: 15px;">
                    <p id="MyFile">
                        <input id="filMyFile" name="filMyFile" size="50" type="file" />
                        </p>
                    </td>
                <td style="width: 448px; height: 15px">
                        <input id="btnadd" οnclick="addFile()" type="button" value="Add"  visible="false" disabled="disabled" />提示:长传的附件不能大于6M</td>
            </tr>
            <tr>
                <td style="width: 108px; height: 15px">
                    文件名称:</td>
                <td style="width: 448px; height: 15px">
                    <asp:TextBox ID="txt_filename" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt_filename"
                        ErrorMessage="请输入文件名称"></asp:RequiredFieldValidator></td>
                <td style="width: 448px; height: 15px">
                </td>
            </tr>
            <tr>
                <td style="width: 108px; height: 15px">
                    上传作者:</td>
                <td style="width: 448px; height: 15px">
                    <asp:TextBox ID="txt_username" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_username"
                        ErrorMessage="请输入你的大名"></asp:RequiredFieldValidator></td>
                <td style="width: 448px; height: 15px">
                </td>
            </tr>
            <tr>
                <td style="width: 108px; height: 15px">
                    备注信息:</td>
                <td style="width: 448px; height: 15px">
                    <asp:TextBox ID="txt_remark" runat="server" Height="98px" TextMode="MultiLine" Width="306px"></asp:TextBox></td>
                <td style="width: 448px; height: 15px">
                </td>
            </tr>
            <tr>
                <td style="width: 108px">
                </td>
                <td style="width: 448px">
                    <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传" CssClass="btn" />
                    <asp:Label ID="lblAttachmentError" runat="server" ForeColor="Red"></asp:Label>
                    <asp:Label
                        ID="lblAttachment" runat="server"></asp:Label></td>
                <td style="width: 448px">
                </td>
            </tr>
        </table>


 

    protected void btnUpload_Click(object sender, EventArgs e)
    {

        HttpFileCollection files = HttpContext.Current.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            if (i < files.Count && i < 10)
            {
                if (files[i].FileName != "" || files[i] != null)
                {
                    int FileSize = 6 * 1024 * 1024;
                    HttpPostedFile myFile = files[i];
                    string strFilePath = myFile.FileName.ToString().Trim();
                    this.lblAttachmentError.Text = "<" + strFilePath + ">"; // Show file name 
                    int nFindSlashPos = strFilePath.Trim().LastIndexOf("//") + 1;
                    string UploadFileName = strFilePath.Substring(nFindSlashPos);
                    string FileName =string.Format("{0:yyMMdd-hhmmss}", DateTime.Now) + "_" + UploadFileName; //this.txtWorkOrder.Text 

                    if (myFile.FileName.Trim() == "") // Empty value in Browse Box
                    {
                        this.lblAttachmentError.Text = "没有文件被选择";
                        return;
                    }

                    if (myFile.ContentLength != 0)
                    {
                        if (myFile.ContentLength > FileSize)
                        {
                            this.lblAttachmentError.Text = "文件大小限制在6M以下";
                            return;
                        }
                        this.lblAttachment.Text += "<BR>" + FileName;
                        this.lblAttachmentError.Text = "";

                        myFile.SaveAs(this.Request.PhysicalApplicationPath.ToString().Trim() + @"/uploads/" + FileName);

                        //保存代码
                        string filename = string.Empty;
                        int  filesize;
                        string filepath = string.Empty;
                        string uploaduser = string.Empty;
                        string remark = string.Empty;

                        filename = txt_filename.Text.Trim();
                        filesize = myFile.ContentLength;
                        filepath = FileName;
                        uploaduser = txt_username.Text.Trim();
                        remark = txt_remark.Text.Trim();

                        string strsql = string.Format("insert onlinedoc(filename,filesize,path,uploaduser,remark)values('{0}',{1},'{2}','{3}','{4}')",filename,filesize,filepath,uploaduser,remark);
                        DABaseAccess db = new DABaseAccess();
                        if (db.GetNonQuery(strsql.ToString()))
                        {
                            JScript.Alert("恭喜你文件上传成功,保存数据库成功!", this.Page);   
                        }
                       else 
                        {
                            JScript.Alert("保存数据库失败,请与管理员龚德辉联系!", this.Page);  
                        }


                    }
                    else
                    {
                        this.lblAttachmentError.Text = "文件没有找到";
                        return;
                    }
                }
            }
            else
                this.lblAttachmentError.Text = "Uploaded File exceed limits.";
        }

    }


 

这个可以同时上传N个 不过我只传一个。

 

下载:

 

 protected void btndownload_Click(object sender, ImageClickEventArgs e)
    {
       
        string path = Server.MapPath("uploads/") + this.path; //   Session["file"].ToString();
        //初始化 FileInfo 类的实例,它作为文件路径的包装
        FileInfo fi = new FileInfo(path);
        //判断文件是否存在
        if (fi.Exists)
        {
            //将文件保存到本机上
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(fi.Name)));
            Response.AddHeader("Content-Length", fi.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(fi.FullName);
            Response.End();
        }
     }


编码采用UTF-8所以标题不会乱码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厦门德仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值