XhEditor上传

首先导入js文件:

View Code
1   <script type="text/javascript" src="../xheditor/jquery/jquery-1.4.4.min.js"></script>
2     <script type="text/javascript" src="../xheditor/xheditor-1.1.10-zh-cn.min.js"></script>
3     <script type="text/javascript">
4         $(function () {
5             $('#txt_content').xheditor({ cleanPaste: 0, tools: 'mfull', skin: 'nostyle', upLinkUrl: '../upload.aspx', upImgUrl: '../upload.aspx', upFlashUrl: '../upload.aspx', upMediaUrl: '../upload.aspx' });
6         })
7     </script>

新建一个upload.aspx文件(后台代码如下):

View Code
  1 using System;
  2 using System.Collections;
  3 using System.Configuration;
  4 using System.Data;
  5 using System.Web;
  6 using System.Web.Security;
  7 using System.Web.UI;
  8 using System.Web.UI.HtmlControls;
  9 using System.Web.UI.WebControls;
 10 using System.Web.UI.WebControls.WebParts;
 11 using System.Text.RegularExpressions;
 12 public partial class upload : System.Web.UI.Page
 13 {
 14     protected void Page_Load(object sender, EventArgs e)
 15     {
 16         //初始化一大堆变量
 17         string inputname = "filedata";    //表单文件域name
 18         string attachdir = "uploads";     // 上传文件保存路径,结尾不要带/
 19         int dirtype = 1;                  // 1:按天存入目录 2:按月存入目录 3:按扩展名存目录  建议使用按天存
 20         int maxattachsize = 419430400;     // 最大上传大小,默认是2M
 21         string upext = "txt,rar,zip,JPG,GIF,jpg,jpeg,bmp,gif,png,swf,wmv,avi,wma,mp3,mid,flv";    // 上传扩展名
 22         int msgtype = 2;                 //返回上传参数的格式:1,只返回url,2,返回参数数组
 23         string immediate = Request.QueryString["immediate"];//立即上传模式,仅为演示用
 24         byte[] file;                     // 统一转换为byte数组处理
 25         string localname = "";
 26         string disposition = Request.ServerVariables["HTTP_CONTENT_DISPOSITION"];
 27 
 28         string err = "";
 29         string msg = "''";
 30 
 31         if (disposition != null)
 32         {
 33             // HTML5上传
 34             file = Request.BinaryRead(Request.TotalBytes);
 35             localname = Regex.Match(disposition, "filename=\"(.+?)\"").Groups[1].Value;// 读取原始文件名
 36         }
 37         else
 38         {
 39             HttpFileCollection filecollection = Request.Files;
 40             HttpPostedFile postedfile = filecollection.Get(inputname);
 41 
 42             // 读取原始文件名
 43             localname = postedfile.FileName;
 44             // 初始化byte长度.
 45             file = new Byte[postedfile.ContentLength];
 46 
 47             // 转换为byte类型
 48             System.IO.Stream stream = postedfile.InputStream;
 49             stream.Read(file, 0, postedfile.ContentLength);
 50             stream.Close();
 51 
 52             filecollection = null;
 53         }
 54 
 55         if (file.Length == 0) err = "无数据提交";
 56         else
 57         {
 58             if (file.Length > maxattachsize) err = "文件大小超过" + maxattachsize + "字节";
 59             else
 60             {
 61                 string attach_dir, attach_subdir, filename, extension, target;
 62 
 63                 // 取上载文件后缀名
 64                 extension = GetFileExt(localname);
 65 
 66                 if (("," + upext + ",").IndexOf("," + extension + ",") < 0) err = "上传文件扩展名必需为:" + upext;
 67                 else
 68                 {
 69                     switch (dirtype)
 70                     {
 71                         case 2:
 72                             attach_subdir = "month_" + DateTime.Now.ToString("yyMM");
 73                             break;
 74                         case 3:
 75                             attach_subdir = "ext_" + extension;
 76                             break;
 77                         default:
 78                             attach_subdir = "day_" + DateTime.Now.ToString("yyMMdd");
 79                             break;
 80                     }
 81                     attach_dir = attachdir + "/" + attach_subdir + "/";
 82 
 83                     // 生成随机文件名
 84                     Random random = new Random(DateTime.Now.Millisecond);
 85                     filename = DateTime.Now.ToString("yyyyMMddhhmmss") + random.Next(10000) + "." + extension;
 86 
 87                     target = "/" + attach_dir + filename;
 88                     try
 89                     {
 90                         CreateFolder(Server.MapPath(attach_dir));
 91 
 92                         System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(target), System.IO.FileMode.Create, System.IO.FileAccess.Write);
 93                         fs.Write(file, 0, file.Length);
 94                         fs.Flush();
 95                         fs.Close();
 96                     }
 97                     catch (Exception ex)
 98                     {
 99                         err = ex.Message.ToString();
100                     }
101 
102                     // 立即模式判断
103                     if (immediate == "1") target = "!" + target;
104                     target = jsonString(target);
105                     if (msgtype == 1) msg = "'" + target + "'";
106                     else msg = "{'url':'" + target + "','localname':'" + jsonString(localname) + "','id':'1'}";
107                 }
108             }
109         }
110 
111         file = null;
112 
113         Response.Write("{'err':'" + jsonString(err) + "','msg':" + msg + "}");
114     }
115 
116 
117     public string jsonString(string str)
118     {
119         str = str.Replace("\\", "\\\\");
120         str = str.Replace("/", "\\/");
121         str = str.Replace("'", "\\'");
122         return str;
123     }
124 
125 
126     public string GetFileExt(string FullPath)
127     {
128         if (FullPath != "") return FullPath.Substring(FullPath.LastIndexOf('.') + 1).ToLower();
129         else return "";
130     }
131 
132     public void CreateFolder(string FolderPath)
133     {
134         if (!System.IO.Directory.Exists(FolderPath)) System.IO.Directory.CreateDirectory(FolderPath);
135     }
136 }

  新建一个uploads文件夹即可

原文地址:https://www.cnblogs.com/Jiangliang/p/2971098.html