文件上传 需要在自己建立的工程里建一个 存储文件的文件夹

 

  

  

控件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());
            }
        }

  

原文地址:https://www.cnblogs.com/haihang/p/2673468.html