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


     

      

      

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

      

  • 相关阅读:
    如何处理DateTime日期时间格式
    ASP.NET访问域用户(AD活动目录)信息的类
    多层代理取真实IP地址
    自动播放MP3文件
    Windows7 下用 grub4dos 安装 Ubuntu
    javascript判断iphone/android手机横竖屏模式
    C#一个到多个Cookie的字符串添加到CookieCollection集合中【isGood代码】
    css 文本对齐4种方法
    让VirtualBox虚拟机实现开机自动后台运行
    MSXML2, XmlHttpClass基础
  • 原文地址:https://www.cnblogs.com/haihang/p/2673468.html
Copyright © 2020-2023  润新知