欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

KindEditor图片上传的Asp.net代码实例

程序员文章站 2024-02-27 12:27:45
复制代码 代码如下:using system;using system.globalization;using system.collections;using syste...

复制代码 代码如下:

using system;
using system.globalization;
using system.collections;
using system.configuration;
using system.data;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
public partial class jscript_kindeditor_upload_cgi_upload : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
//文件保存目录路径
string savepath = "/upload_images/";
//文件保存目录url
string saveurl = "/upload_images/";
//上传图片类型
string[] extstr=new string[4];
extstr[0] = ".gif";
extstr[1] = ".jpg";
extstr[2] = ".png";
extstr[3] = ".bmp";
//图片的最大大小
int maxsize = 100000;
//错误提示
string[] msgstr = new string[3];
msgstr[0] = "上传文件大小超过限制.";
msgstr[1] = "上传文件扩展名是不允许的扩展名.";
msgstr[2] = "上传文件失败//n请重试.";
string imgwidth = request.form["imgwidth"];
string imgheight = request.form["imgheight"];
string imgborder = request.form["imgborder"];
string imgtitle = request.form["imgtitle"];
string imgalign = request.form["imgalign"];
string imghspace = request.form["imghspace"];
string imgvspace = request.form["imgvspace"];
httppostedfile imgfile = httpcontext.current.request.files["imgfile"];
//获得文件名
string filename = system.io.path.getfilename(imgfile.filename);
if (filename != "")
{
if (imgfile.contentlength > maxsize)
{
alert(msgstr[0]);
}
else
{
string fileextension = system.io.path.getextension(filename).tolower();
if (checkext(extstr, fileextension))
{
//重新为文件命名,时间毫秒部分+扩展名
string imgrename = "" + datetime.now.tostring("yyyy-mm-dd-hh-mm-ss-ffff", datetimeformatinfo.invariantinfo) + "" + fileextension;
//文件夹名
string imgfoldername=datetime.now.tostring("yyyymmdd",datetimeformatinfo.invariantinfo);
try
{
if (!system.io.directory.exists(@server.mappath("" + savepath + "" +imgfoldername + "")))
{
//生成文件完整目录
system.io.directory.createdirectory(@server.mappath("" + savepath + "" +imgfoldername + ""));
}
imgfile.saveas(@server.mappath("" + savepath + "" + imgfoldername + "/")+imgrename);

}
catch
{
alert(msgstr[2]);
}
string imgurl = saveurl + imgfoldername + "/" + imgrename;
returnimg(imgurl, imgwidth, imgheight, imgborder, imgtitle, imgalign, imghspace, imgvspace);
}
else
{
alert(msgstr[1]);
}
}
}

}
/// <summary>
/// 提示关闭层
/// </summary>
/// <param name="msgstr"></param>
private void alert(string msgstr)
{
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(/"" + msgstr + "/");parent.kinddisablemenu();parent.kindreloadiframe();</script>");
response.write("</body>");
response.write("</html>");
}
/// <summary>
/// 检测文件类型
/// </summary>
/// <param name="extstr"></param>
/// <param name="fileext"></param>
/// <returns></returns>
private bool checkext(string[] extstr,string fileext)
{
for (int i = 0; i < extstr.length; i++)
{
if (extstr[i] != fileext)
{
return true;
}
}
return false;
}
/// <summary>
/// 返回图片
/// </summary>
/// <param name="fileurl"></param>
/// <param name="filewidth"></param>
/// <param name="fileheight"></param>
/// <param name="fileborder"></param>
/// <param name="filetitle"></param>
/// <param name="filealign"></param>
/// <param name="filehspace"></param>
/// <param name="filevspace"></param>
private void returnimg( string fileurl,string filewidth,string fileheight,string fileborder,string filetitle,string filealign,string filehspace,string filevspace)
{
response.write("<html>");
response.write("<head>");
response.write("<title>上传成功</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.kindinsertimage(/"" + fileurl +"/",/"" + filewidth + "/",/"" + fileheight + "/",/"" + fileborder + "/",/"" + filetitle + "/",/"" + filealign + "/",/"" + filehspace + "/",/"" + filevspace + "/");</script>");
response.write("</body>");
response.write("</html>");
}
}