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

      

  • 相关阅读:
    [轉]Flex实现代码分离mxml/as
    [轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
    [轉]mysqli & pdo使用实例和详解
    JSON格式驗證以及格式說明
    [轉]can't return a result set in the given context及参数解释
    PHP ADODB資源
    [轉]Virtual PC 网络设置(Networking)
    [轉]20个非常有用的PHP类库
    [轉]MySQL存储过程 ERROR Handler 异常处理
    [轉]如何通过Jquery获取radio的值
  • 原文地址:https://www.cnblogs.com/haihang/p/2673468.html
Copyright © 2011-2022 走看看