asp.net 新闻发布多附件上传

6 篇文章 0 订阅
3 篇文章 0 订阅

使用 input file 实现多附件上传,注意form中必须添加 enctype="multipart/form-data"  method="post"


<form id="form1" runat="server" enctype="multipart/form-data"  method="post">
        <div>
            <input type="hidden" runat="server" id="hidNewsCategories" />
            <input type="hidden" runat="server" id="hidNewsID" />    
            <input type="hidden" runat="server" id="hidOperate" /> 
            <input type="hidden" runat="server" id="hidResult" />    
                
        </div>
        
        
        <div class="toolbar">
            <input id="btnSaveShow" type="button" value="保 存" οnclick="CheckInput();" class="blue" />
            <asp:Button ID="btnSave" runat="server" Text="保 存" OnClick="btnSave_Click" Style="display: none;" />
            <input id="btnView" type="button" value="预览" οnclick="view();" class="blue" />
            <asp:Button ID="btnBack" runat="server" Text="返 回" class="blue" OnClick="btnBack_Click"  />
    </div>
    <div class="breadcrumb newscontainer" >
        <div class="crumbtitle">
            发布新闻
        </div>
        <ul class="crumbcontent">
            
            <li>
                <span class="clsSpan">新闻类型:</span>
                <asp:DropDownList ID="ddlNewsCategories" runat="server" class="ddl"></asp:DropDownList>         
                </li>       
            <li>
                <span class="clsSpan">新闻标题:</span>
                <input id="txtNewsTitle" type="text" runat="server" class="input inputWidth" maxlength="50" />  
                <span style="color:red">*</span>              
            </li>
            <li>
                <span class="clsSpan">副标题:</span>
                <input id="txtSubtitle" type="text" runat="server" class="input inputWidth" maxlength="50"/>
            </li>
    
            
            <li>
                <CKEditor:CKEditorControl ID="CKEditorControl1" runat="server" Height="240px" ></CKEditor:CKEditorControl>
            </li>

            <li>
            <span class="clsSpan">
                <a id="attach" href="javascript:;" οnclick="addFj()" title="请选择正确图片格式:jpg,jpeg,png,bmp,gif">添加图片</a>
                </span>
            <div id="divFjShow" runat="server" style="width:100%;">
                <ul id="ulFjShow" class="acc" runat="server">
                  
                    
                </ul>
               

            </div>
                <div id="divFj"  style="width:100%;">
                 <ul id="ulFj" >                  
                    
                </ul>

            </div>
            </li>
                <li>
                        <span class="clsSpan" runat="server" id="hidSpan" style="display:none;">
                <a id="a1" href="javascript:;" οnclick="addFj()" style="color:blue;" title="请选择正确图片格式:jpg,jpeg,png,bmp,gif">添加图片</a>
                </span>
                    </li>
    
        </ul>
    </div>
                    
        </form>

调用js:


function iniPage() {
    if ($("#ulFjShow li").length > 0) {
        document.getElementById("hidSpan").style.display = "block";
    }
}

window.onload = iniPage;


function change(o, txtid) {
    //o.value就是你选择的文件的完整路径,然后你可以自己过滤
    var isAllow = false;
    var strAllowFile = "jpg,jpeg,png,bmp,gif";
    var allowFiles = new Array();
    allowFiles = strAllowFile.split(",");
    var strFile = o.value
    var strs = new Array(); //定义一数组
    strs = strFile.split("."); //字符分割      
    if (strs.length > 1) {
        var strHz = strs[strs.length - 1].toLowerCase();
        for (var i = 0; i < allowFiles.length; i++) {
            if (strHz == allowFiles[i]) {
                isAllow = true;
            }

        }
    }
    if (isAllow == false) {
        o.value = "";
        alert("请选择图片类型!");
    }
    else {
        txtid = 'txt' + txtid;
        var txt = document.getElementById(txtid);
        if (txt.value.length == 0) {
            var pos = o.value.lastIndexOf('\\');
            txt.value = o.value.substring(pos + 1);
        }
    }
}

function delFj(n) {
    alert(n);
    var ul = document.getElementById("ulFj");
    document.getElementById("ulFj").removeChild(document.getElementById(n));
    if ($("#ulFjShow li").length == 0 && $("#ulFj li").length == 0) {
        document.getElementById("hidSpan").style.display = "none";
    }
}

function delFjShow(n) {

    var ul = document.getElementById("ulFjShow");
    document.getElementById("ulFjShow").removeChild(document.getElementById(n));
    if ($("#ulFjShow li").length == 0 && $("#ulFj li").length == 0) {
        document.getElementById("hidSpan").style.display = "none";
    }
}

var id = 0;
function addFj() {
    document.getElementById("hidSpan").style.display = "block";
    var ul = document.getElementById("ulFj");
    var li = document.createElement("li");

    id = id + 1;
    var h = '文件描述:<input id="txt' + id + '" name="txt" type="text" class="input descWidth"/>';
    h += '  <input id="file' + id + '" type="file" οnchange="change(this,' + id + ')" name="File" style="height:23px;width:300px;"/>';
    h += '<input type="button" value="删除" οnclick="delFj(' + id + ')" />';
    h += '<input id="hidfile' + id + '" name="hidfile' + id + '" type="hidden" value="' + id + '" style="height:23px;" />';

    li.innerHTML = h;
    li.id = id;
    ul.appendChild(li);    
}

后台保存代码:

for (int i = 0; i < Request.Files.Count; i++)
            {
                if (Request.Files[i].ContentLength > 0)
                {
                    filePath = Request.Files[i].FileName;
                    string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                    string[] strArry = fileName.Split('.');
                    string strFileType = strArry[1];
                    string strNewFileName = DateTime.Now.ToString("yyMMddHHmmss") + i.ToString();
                    string strPath = "UpLoad\\News";//相对路径
                                                    
                    string strSaveFilePath = strPath + "\\" + strNewFileName + strFileType;
                    string strFPath = HttpContext.Current.Server.MapPath("~/") + strSaveFilePath;
                    if (File.Exists(strFPath))
                    {
                        File.Delete(strFPath);
                    }

                    Request.Files[i].SaveAs(strFPath);

                    if (string.IsNullOrEmpty(strPath))
                    {
                        MessageBox.Show(this, "show", "请选择正确的图片格式!");
                        return false;
                    }

                    DataRow drFileNew = dtFiles.NewRow();
                    drFileNew["ID"] = Guid.NewGuid().ToString();
                    drFileNew["BusinessID"] = strId;
                    drFileNew["Categories"] = "IMG";
                    drFileNew["FileType"] = strFileType;
                    if (strFileDescArry[i].Trim() != "")
                    {
                        drFileNew["FileDesc"] = strFileDescArry[i];
                    }
                    else
                    {
                        drFileNew["FileDesc"] = fileName;
                    }
                    drFileNew["FileName"] = fileName;
                    drFileNew["Path"] = strPath;
                    drFileNew["UploadTime"] = DateTime.Now;
                    drFileNew["OrderID"] = orderId;
                    dtFiles.Rows.Add(drFileNew);
                }

            }
            return fileOper.SaveFiles(dtFiles);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值