上传图片文件和在线文本编辑器的使用

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Vote_SubjectAdd.aspx.cs" Inherits="oa_vote_Vote_SubjectAdd" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>添加投票主题</title>
    <link href="../../common/css/common.css" rel="stylesheet" type="text/css" />
    <link href="../../common/jquery-easyui-1.4.1/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../common/jquery-easyui-1.4.1/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../../common/js/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="../../common/js/common.js" type="text/javascript"></script>
    <script src="../../common/DatePicker/WdatePicker.js" type="text/javascript"></script>
    <script src="../../common/jquery-easyui-1.4.1/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../common/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../../common/js/jquery.urldecoder.min.js" type="text/javascript"></script>
    <script src="../../common/js/loading.js" type="text/javascript"></script>
        <script src="../../common/kindeditor/kindeditor-min.js" type="text/javascript"></script>
    <script src="../../common/kindeditor/lang/zh_CN.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            KindEditor.ready(function (K) {
                editor = K.create('#txtMainContent', {
                    cssPath: '../../common/kindeditor/plugins/code/prettify.css',
                    uploadJson: '../../common/kindeditor/asp.net/upload_json.ashx',
                    fileManagerJson: '../../common/kindeditor/asp.net/file_manager_json.ashx',
                    allowFileManager: true,
                    items: [
'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
'insertunorderedlist', '|', 'emoticons', 'image', 'link']

                });
            });

        })

 
 


        function clearNoNum(obj) {
            obj.value = obj.value.replace(/[^d.]/g, ""); //清除“数字”和“.”以外的字符
            obj.value = obj.value.replace(/^./g, ""); //验证第一个字符是数字而不是.
            obj.value = obj.value.replace(/.{2,}/g, "."); //只保留第一个. 清除多余的.
            obj.value = obj.value.replace(".", "$#$").replace(/./g, "").replace("$#$", ".");
        }
        function back() {
            location.href = "Vote_SubjectList.aspx";
        }

        function setImageView(obj) {
            var array = new Array('gif', 'jpeg', 'png', 'jpg', 'bmp'); //可以上传的文件类型  
            if (obj.value == '') {
                alert("选择要上传的图片!");
                return false;
            }
            document.getElementById("hfImgTrue").value = "false";
            var fileContentType = obj.value.match(/^(.*)(.)(.{1,8})$/)[3]; //这个文件类型正则很有用   
            ////布尔型变量  
            var isExists = false;
            //循环判断图片的格式是否正确
            for (var i in array) {
                if (fileContentType.toLowerCase() == array[i].toLowerCase()) {
                    document.getElementById("hfImgTrue").value = "true";
                    isExists = true;
                }
            }
            if (isExists == false) {
                alert("上传图片类型不正确!");
                return false;
            }
            var file = obj.files[0];
            var blob = (typeof file === 'string') ? file : URL.createObjectURL(file);
            var img = document.getElementById("preview");
            img.src = blob;
        }
        //确认保存信息
        function CheckResult() {



            var idFile = document.getElementById("txtFileUpload").value;
            //判断是否选择图片  
            if (idFile == null || idFile == "") {
                alert('请添加图片!');
                return false;
            }

            var isImageTrue = document.getElementById("hfImgTrue").value;
            if (isImageTrue == "false") {
                alert('上传图片格式不正确!');
                return false;
            }
            return true;


        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="title" style ="overflow:hidden;">添加投票主题</div> 


        <table class="commontab" border="0" cellpadding="0" cellspacing="0" style=" 100%;
            overflow: auto;">
            <tr>
                <td style="text-align: right;  151px; height: 30px;">
                    投票主题:
                    
                </td>
                <td style="text-align: left; padding-left: 1px;">
                    
                    <input id="txtname"  runat="server" class="txt" style=" 250px;" />
                    
                </td>
            </tr>
            <tr>
                <td style="text-align: right;  151px; height: 30px;">
                    开始时间:
                </td>
                <td style="text-align: left; padding-left: 1px;">
                    <input id="startTime" runat="server" type="text" class="txt date" style=" 150px;"
                         onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" />
                </td>
            </tr>
            <tr>
                <td style="text-align: right;  151px; height: 30px;">
                    结束时间:
                </td>
                <td style="text-align: left; padding-left: 1px;">
                    <input id="endTime" runat="server" type="text" class="txt date" style=" 150px;"
                         onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" />
                </td>
            </tr>
            <tr>
                <td style="text-align: right;  151px; height: 30px;">
                    投票个数:
                </td>
                <td style="text-align: left;  500px; padding-left: 1px;">
                    <input id="counts" onkeyup="clearNoNum(this)"  runat="server" class="txt" style=" 250px;" />&nbsp;&nbsp;<span style="color:red;">个数为0时,不做限制</span>
                </td>
            </tr>
            <tr>
                <td style="text-align: right;  151px; height: 30px;">
                    上传主题图片:
                </td>
                <td style="text-align: left; padding-left: 1px;">
                    <asp:FileUpload ID="txtFileUpload" Width="200" runat="server" onchange="setImageView(this)" />&nbsp;&nbsp;<span style="color:red;">上传图片最佳分辨率:1080*768</span>
                </td>
            </tr>
            <tr>
                <td style="text-align: right;  151px; height: 30px;">
                    
                </td>
                <td style="text-align: left;  500px; padding-left: 1px;">
                        <div id="localImag" style=" 100px; height: 100px;">
                            <img id="preview" alt="预览图片" src="Image/other_1.PNG" width="150" height="107"
                                runat="server" />
                        </div>
                </td>
            </tr>
            <tr>
                <td style="text-align: right;  151px; height: 30px;">
                    投票简介:
                </td>
                <td style="text-align: left; padding-left: 1px;">
                    
                    <textarea id="txtMainContent" runat="server" cols="100" rows="8" style="80%;min-350px;height:300px;visibility:hidden;"></textarea>
                </td>
            </tr>


        </table>


        <div class="brline"></div>
        <table class="commontab" style="table-layout: fixed;  100%;">
            <tr>
                <td class="br" style="height: 50px; padding-left: 200px;">
                    <%--<a class="l-btn" href="javascript:void(0);" onclick="back();"><span class="l-btn-left">
                        <img src="/public/Themes/Images/arrow_left.png" alt="" />返 回</span></a>--%>
                        <asp:Panel id="Panel1" runat="server">
                    
                        <asp:Button ID="Button1" runat="server" Text="保 存" class="btn"
                        style="60px;height:25px;" onclick="baocunqj_Click" />
                            &nbsp;&nbsp;&nbsp;&nbsp;<input id="Button3" class="btn" type="button" style="60px;height:25px;" onclick="back();" value="返 回" /></asp:Panel> 
                </td>
            </tr>
        </table>
      <asp:HiddenField ID="hfImageURL" runat="server" />
    <asp:HiddenField ID="hfImgTrue" runat="server" />
//上面2个隐藏值要注意申明
</form> </body> </html>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.oa;
using System.Data;
using System.IO;
using System.Text;

public partial class oa_vote_Vote_SubjectAdd : System.Web.UI.Page
{
    Vote_SubjectBLL vote = new Vote_SubjectBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            counts.Value = "0";
        }
    }
    protected void baocunqj_Click(object sender, EventArgs e)
    {
        if (this.startTime.Value == "")
        {
            MessageBox.Show("开始时间不能为空!");
            return;
        }
        if (this.endTime.Value == "")
        {
            MessageBox.Show("结束时间不能为空!");
            return;
        }
        if (Convert.ToDateTime(this.startTime.Value) > Convert.ToDateTime(this.endTime.Value))
        {
            MessageBox.Show("开始时间不得晚于结束时间!");
            return;
        }

        string sysId = Guid.NewGuid().ToString();
        string name = txtname.Value;
        string startTime1 = startTime.Value;
        string endTime1 = endTime.Value;
        string counts1 = counts.Value;
        if (counts1.Length <= 0)
        {
            counts1 = "0";
        }
        string isCurrent1 = "1";
        string intro1 = txtMainContent.Value;
        string creatorId = SessionInclude.Id;//当前用户ID号
        string createDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        string image_big = "";

        if (txtFileUpload.PostedFile.FileName != "")
        {
            //上传文件到服务器
            string filename = sysId + "_2";
            string houzhui = txtFileUpload.PostedFile.FileName.Substring(this.txtFileUpload.PostedFile.FileName.LastIndexOf(".") + 1);
            if (!Directory.Exists(Server.MapPath("../../upload/vote/theme/" + sysId)))
            {
                Directory.CreateDirectory(Server.MapPath("../../upload/vote/theme/" + sysId));
            }
            txtFileUpload.PostedFile.SaveAs(Server.MapPath("../../upload/vote/theme/" + sysId + "/" + filename + "." + houzhui));

            image_big = "<img style="50px;height:40px;" src="../../upload/vote/theme/" + sysId + "/" + filename + "." + houzhui + "" />";

            //image_big.Replace("\", "");
        }


        int result = vote.AddOA_Vote_SubjectList(sysId, name, startTime1, endTime1, counts1, isCurrent1, intro1, creatorId, createDate, image_big);

        if (result > 0)
        {
            Response.Redirect("Vote_SubjectList.aspx");
        }
        else
        {
            MessageBox.Show("操作失败!");
            return;
        }
    }
}
原文地址:https://www.cnblogs.com/Zpyboke/p/5241104.html