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;
public partial class kindeditor_aspx_upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//文件保存目录路径
String savePath = "./../../Upimages";
//文件保存目录URL
String saveUrl = "./../Upimages/";
//最大文件大小
int maxSize = 0;
HttpPostedFile file = Request.Files.Get("imgFile");
if (file.ContentLength > 0)
{
maxSize = file.ContentLength / 1024 / 1024;
if (maxSize > 2)
{
this.Alert("上传文件大小超过限制。");
return;
}
//提前原文件路径和名字 如 f:\123\images\like.jpg
String allFileName = file.FileName;
//提取原文件名 "\\"为转移字符等于\ 提取以后文件名等于 like.jpg
String testName = allFileName.Substring(allFileName.LastIndexOf("\\") + 1);
//提取文件后缀
String postfix = allFileName.Substring(allFileName.LastIndexOf(".") + 1).ToLower();
if (postfix != "jpg" && postfix != "gif" && postfix != "bmp" && postfix != "png")
{
this.Alert("上传文件扩展名是不允许的扩展名。");
return;
}
try
{
string file_name = this.GetFilePrefix() + "." + postfix;
file.SaveAs(Server.MapPath(savePath) + "\\" + file_name);
string file_url = saveUrl + file_name;
Response.Write("<html>");
Response.Write("<head>");
Response.Write("<title>Insert Image</title>");
Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
Response.Write("</head>");
Response.Write("<body>");
Response.Write("<script type=\"text/javascript\">parent.KE.plugin[\"image\"].insert(\"' . "+Request["id"]+" . '\", \"' . "+file_url+" . '\",\"' ."+Request["imgTitle"]+" . '\",\"' . "+Request["imgWidth"]+" . '\",\"' . "+Request["imgHeight"]+" . '\",\"' ."+Request["imgBorder"]+" . '\");</script>");
Response.Write("</body>");
Response.Write("</html>");
}
catch
{
this.Alert("上传失败");
return;
}
}
}
protected void Alert(string msg)
{
Response.Write("<html>");
Response.Write("<head>");
Response.Write("<title>error</title>");
Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
Response.Write("</head>");
Response.Write("<body>");
Response.Write("<script type=\"text/javascript\">alert(\"'"+msg+"'\");history.back();</script>");
Response.Write("</body>");
Response.Write("</html>");
}
/// <summary>
/// 获取一个随机产生的字符串
/// </summary>
/// <returns>得到的字符串</returns>
private string GetFilePrefix()
{
String str = DateTime.Now.ToString("yyyyMMddhhmmssfffffff").ToString(); ;
return str;
}
}