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

      

  • 相关阅读:
    每天1题算法题(4)-合并二叉树 (√)
    每天1题算法题(3)-二叉树的最大深度 (√)
    每天1题算法题(2)-二叉树的层序遍历
    每天1题算法题(1)-二叉树的中序遍历
    uni-app登录页白屏
    uni-app真机运行顶部导航栏右边按钮显示不全的问题处理
    uni-app强制横屏设置
    uni-app离线打包步骤
    银行数据仓库体系实践(18)--数据应用之信用风险建模
    银行数据仓库体系实践(17)--数据应用之营销分析
  • 原文地址:https://www.cnblogs.com/haihang/p/2673468.html
Copyright © 2011-2022 走看看