zoukankan      html  css  js  c++  java
  • 文件上传 需要在自己建立的工程里建一个 存储文件的文件夹

     

      

      

    控件js代码
    <script>
            function AddAttachments() {
                document.getElementById('attach').innerText = "继续添加附件";
                tb = document.getElementById('attAchments');
                newRow = tb.insertRow();
                newRow.insertCell().innerHTML = "<input class='InputCss' name='File' size='82' type='file' contenteditable='false'>  <input id='deleteInfo' class='ButtonCss' type=button value='删除' onclick='delFile(this.parentElement.parentElement.rowIndex)'><br>";
                UploadFileLabel.Text = "";
            }
            function delFile(index) {
                document.getElementById('attAchments').deleteRow(index);
                tb.rows.length > 0 ? document.getElementById('attach').innerText = "添加附件" : document.getElementById('attach').innerText = "继续添加附件";
                UploadFileLabel.Text = "";
            }
     
        </script>
    

      

    前台
    <tr>
                        <td height="30" align="right" class="textstyle">
                            上传附件 :
                        </td>
                        <td colspan="2">
                              <table id="attAchments" width="100%" class="gbtext">
                                            <tr id="tr" runat="server">
                                                <td class="style1">
                                                    <a id="attach" href="javascript:;" name="attach" onclick="AddAttachments();" style="font-size: 9pt;
                                                        font-family: 宋体" title="添加附件"><span style="text-decoration: underline;">添加附件</span></a>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:Label ID="UploadFileLabel" runat="server" ForeColor="Red"></asp:Label>
                                                </td>
                                            </tr>
                                        </table>
                        </td>
                    </tr>
    

      

     千万要注意表单里的东西怎么写 要不然获得的文件数量始终为0
    
    
    <form id="form1" runat="server" enctype="multipart/form-data">
    

      

    插入后台代码
    
    HttpFileCollection fc = Request.Files;
             for (int i = 0; i < fc.Count; i++)
            {
                if (fc[i].FileName != "" && fc[i] != null && fc[i].ContentLength > 0)
                {
                    string stri = "insert into Common_Files(UserID,OtherFileID,FileTrueName,FileName,FileOtherName,FileLength,CreateTime,CreateUser)Values('haihang','" + Guid.NewGuid().ToString() + "','" + fc[i].FileName.Substring(fc[i].FileName.LastIndexOf('\\') + 1) + "','" + Guid.NewGuid().ToString() + "','" + Guid.NewGuid().ToString() + "','" + fc[i].ContentLength.ToString() + "','" + DateTime.Now + "','haihang')";
                    int cfliesid = Convert.ToInt32(SQLHelper.ExecuteScalar(stri));
                    string stri1 = "insert into Common_Files_Relation(FileID,ID,FileType)Values('" + cfliesid + "','" + aid + "','Thesis')";
                    SQLHelper.ExecuteScalar(stri1);
                    string path = Server.MapPath("~\\FilesUpload\\UploadFiles") + "\\";
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    fc[i].SaveAs(path + Guid.NewGuid().ToString());
                }
            }
    

      

  • 相关阅读:
    Linux之20——sudo命令
    Linux之18——Linux下安装MySQL及远程连接MySQL
    Linux之17——Git安装及使用以及连接GitHub方法详解
    Linux之16——free性能调优命令
    Linux之15——nc命令详解
    Linux之14——curl命令详解
    Linux之13——常用统计命令之wc
    15 Python 迭代器和生成器
    16 Python 递归函数
    17 python 内置函数
  • 原文地址:https://www.cnblogs.com/haihang/p/2673468.html
Copyright © 2011-2022 走看看