• 实现对附件的操作.


    在html页面中定义附件初始情况

    <td style="background:#F3F8FD; border-top-style: none; border-top-color: inherit; border-top- medium;" 
                        class="style1">
                    附件:
                </td>
                <td  colspan="3"; style="background:#F3F8FD;795px;padding-left:1px;border-top:none;">
                    <asp:Literal ID="attachementLite" runat="server"></asp:Literal>
                    <table id="upDiv" border="0" style="margin-bottom: 5px;">
                        <tr>
                            <td>
                                <input type="file" name="upfile" style=" 190px;height:20px;" class="txt" />
                            </td>
                            <td align="right">
                                <span style="padding-left: 2px;">
                                    <input type="button" onclick="addFile();" class="btn" name="next" value="增加" style=" 48px;
                                        height: 18px; line-height: 16px;" /></span>
                            </td>
                        </tr>
                    </table>
                </td>

    后台读取附件表数据,并加载到前台显示

                FileUploadBLL fileUploadBll = new FileUploadBLL();
                IList<Model.FileUpload> attachmentList = fileUploadBll.GetListByEntityId(sysid);//将查询得到的结果集存到IList中
                StringBuilder strAttachment = new StringBuilder();
                if (attachmentList != null)
                {
                    foreach (Model.FileUpload fup in attachmentList)//循环Model FileUpload的数据到定义的fup中.
                    {
                        strAttachment.Append("<span id="div" + fup.sysId + ""><a href="#" onclick="downLoadFilesel('" + fup.sysId + "')">" + fup.name + "</a>&nbsp;&nbsp;<a style="cursor:pointer" onclick="delFile('" + fup.sysId + "')">删除</a></span><br />");
                    }
                    if (attachmentList.Count > 0)
                    {
                        attachementLite.Text = strAttachment.ToString();
                    }
                }

    定义对附件的删除,新增附件选框,删除附件选框的js方法

                function delFile(fileId) {
    //删除之前将对应的附件项隐藏,表现出删除的效果 //异步删除附件,用ajax,把附件id传到另一个页面,另一个页面的后台接收id,根据id进行删除操作
    var vardiv = "div" + fileId; $("#" + vardiv + "").hide(); $.ajax({ type: "POST", url: "../../pact/delFile.aspx?fileId=" + fileId, success: function (msg) { } }); } function addFile() {
    //通过DOM对象来生成新的附件选框,绑定当前选框的删除按钮
    var num = document.getElementsByName("upfile").length + 1; var tb = document.getElementById('upDiv'); var tr = document.createElement("tr"); var td = document.createElement("td"); //var upFile = document.createElement("<input type='file' name='upfile'>"); var upFile = document.createElement("input"); upFile.setAttribute("type", "file"); upFile.setAttribute("name", "upfile"); //var href = document.createElement("<a style="cursor:pointer" onclick=removeFile(this)></a>"); var href = document.createElement("a"); href.setAttribute("onclick", "javascript:removeFile(this)"); href.innerHTML = "<input type="button" class="btn" name="next" value="删除" style=" 48px;height: 18px; line-height: 16px;" />"; /*td.insertAdjacentElement('beforeEnd',upFile); td.insertAdjacentElement('beforeEnd',href); tr.insertAdjacentElement('beforeEnd', td);*/ td.appendChild(upFile); td.appendChild(href); tr.appendChild(td); //tb.childNodes[0].insertAdjacentElement('beforeEnd', tr); tb.appendChild(tr); if (num >= 9) { //document.getElementById("Remove").style.display="none"; } } function removeFile(obj) {
    //删除按钮对象的父级对象的父级对象,对于父节点的处理参照
    //document.getElementById('upDiv').childNodes[0].removeChild(obj.parentElement.parentElement); document.getElementById('upDiv').removeChild(obj.parentElement.parentElement); var num = document.getElementsByName("upfile").length + 1; if (num <= 9) { // document.getElementById("Remove").style.display="block"; } }

    处理方法

    在项目下的一个页面内对js方法中传来的附件id以及其它值进行处理

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using ChengJian.Com.UI;
    
    public partial class pact_delFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["fileId"] != null)
            {
                string fileId = Request.QueryString["fileId"].ToString();
                FileUploadUtil.deleteFileUpload(fileId);
                //BaseFunction bFunction = new BaseFunction();
                //bFunction.ExecuteNonQuery("delete from chengjian_FileUpload where sysId = '" + fileId + "'");
            }
    
        }
    }
            /// <summary>
            /// 删除单个附件表
            /// </summary>
            /// <param name="sysId"></param>
            public static void deleteFileUpload(string sysId)
            {
                FileUploadBLL fileUploadBll = new FileUploadBLL();
                Model.FileUpload fup = fileUploadBll.GetModel(sysId);
                if (fup != null)
                {
                    string uploadpath = string.Empty;
                    uploadpath = ConfigHelper.GetConfigStr("uploadpath");
                    uploadpath += fup.path + "/" + fup.uploadName;
                    FileInfo file = new FileInfo(uploadpath);
                    if (file.Exists)
                    {
                        file.Delete(); //删除实际附件
                    }
    
                    fileUploadBll.Delete(sysId); //删除表记录
                }
            }
    
    
    
     

    添加好附件之后的保存的写法.

                if (result)
                {
                    UploadWebPage sda = new UploadWebPage();
                    sda.UploadFile("investments", sysid, SessionInclude.Id);//第一个字符串是附近存放的文件夹.没有该文件夹会自动创建,sysid是附件表的主键id,自己定义,在后面的参数则是添加附件的用户id
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('操作成功!');window.location.href ="investmentPlanEdit.aspx?SysId=" + sysid + ""</script>");
    
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('操作失败!');</script>");
                }
    对应的BLL
            public void UploadFile(string folder, string entityId, string creatorId)
            {
                UploadFile(null, null, folder, entityId, creatorId);
            }
    对应的的DAL
            public void UploadFile(System.Web.HttpRequest request, string dir, string folder, string entityId, string creatorId)
            {
                if (request == null)
                {
                    request = Request;
                }
    
                string inspath = folder + """ + entityId;
    
                for (int i = 0; i < request.Files.Count; i++)//循环添加附件
                {
                    if (request.Files[i].ContentLength > 0)
                    {
                        string fileName = System.IO.Path.GetFileName(request.Files[i].FileName);//获得上传附件名.
    
                        string path = "";
                        if (dir == null || dir == string.Empty)
                        {
                            path = ConfigHelper.GetConfigStr(UPLOADPATH);//设定文件路径
                        }
                        else
                        {
                            path = ConfigHelper.GetConfigStr(dir);
                        }
    
                        path += folder + "/" + entityId;
    
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        string _path = path + "/" + fileName;
                        string sysId = UUIDGenearte.getUUID();
                        path += "/" + sysId + System.IO.Path.GetExtension(_path);
    
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
    
                        request.Files[i].SaveAs(path);
    
                        fileUpload = new FileUpload();
                        fileUpload.sysId = sysId;
                        fileUpload.entityId = entityId;
                        fileUpload.creatorId = creatorId;
                        fileUpload.uploadType = folder;
                        fileUpload.contentType = request.Files[i].ContentType;
                        fileUpload.fileSize = request.Files[i].ContentLength;
                        fileUpload.name = fileName;
                        fileUpload.path = folder + "/" + entityId;
                        fileUpload.uploadName = fileUpload.sysId + System.IO.Path.GetExtension(_path);
                        fileUpload.createdDate = DateTime.Now;
    
                        fileUploadBll.Add(fileUpload);//将数据存入附件表中
                    }
                }
            }
    
    
    
    
    
    
    
    
     
  • 相关阅读:
    asp.net微信内置浏览器下Session失效
    iOS 开发之路(WKWebView内嵌HTML5之图片上传) 五
    移动Web开发(二)
    iOS 开发之路(使用WKWebView加载Html5) 四
    iOS 开发之路(AES/DES加密实现) 三
    移动Web开发(一)
    iOS 开发之路(登陆验证调用WebService)二
    iOS 开发之路(登陆页键盘遮挡输入框问题)一
    canvas绘画常用方法
    JavaScript函数定义方式
  • 原文地址:https://www.cnblogs.com/Zpyboke/p/5157017.html
Copyright © 2020-2023  润新知