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

来源:互联网 发布:aframe.js obj 编辑:程序博客网 时间:2024/05/27 02:30

使用 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="保 存" onclick="CheckInput();" class="blue" />            <asp:Button ID="btnSave" runat="server" Text="保 存" OnClick="btnSave_Click" Style="display: none;" />            <input id="btnView" type="button" value="预览" onclick="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:;" onclick="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:;" onclick="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" onchange="change(this,' + id + ')" name="File" style="height:23px;width:300px;"/>';    h += '<input type="button" value="删除" onclick="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);



原创粉丝点击