KindEditor图片上传的Asp.net代码实例_实用技巧

复制代码 代码如下:

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>");
}
}

时间: 2024-09-17 02:54:16

KindEditor图片上传的Asp.net代码实例_实用技巧的相关文章

基于ASP.NET+EasyUI框架实现图片上传提交表单功能(js提交图片)_实用技巧

我的风格,先给大家展示下效果图,具体效果图如下所示,如果大家感觉还不错很满意请参考实现代码. HTML的代码: <form id="ff" runat="server" method="post"> <div id="content" style="margin-left:50px;"> <table style="width:300px;" id=&quo

ASP.NET MVC图片上传前预览简单实现_实用技巧

本文实例为大家分享了图片上传前预览并获取图片文件名和图片字节大小的具体实现代码,供大家参考,具体内容如下 在控制器中创建一个Action: 在Views目录下对应的控制器名称下创建视图PreViewing: 上图中 标记1,引用jQuery类库.标记2,选择文件.标记3,预览图片.标记4,显示图片文件名和图片字节大小.标记5,是Javascript代码...... 本例预览: 以上就是本文的全部内容,希望对大家的学习有所帮助.

asp.net图片上传生成缩略图的注意事项_实用技巧

bitmap.Save(imgPath,ImageFormat.Jpeg);   //这是保存缩略图的一段代码,其中的ImageFormat.Jpeg一定不能省略,即使你保存的文件本来就是jpg格式的,也不能去掉.因为如果去掉的话,生成的缩略图比原始图片还要大! //另外,imgPath必须首先创建,否则会产生GDI+的一般性错误. path=System.Web.HttpContext.Current.Server.MapPath(path); 使用if(!System.IO.Director

asp.net文件上传解决方案(图片上传、单文件上传、多文件上传、检查文件类型)_实用技巧

小编之前也介绍了许多ASP.NET文件上传的解决案例,今天来个asp.net文件上传大集合. 1 使用标准HTML来进行图片上传 前台代码: <body> <form id="form1" runat="server"> <div> <table> <tr> <td colspan="2" style="height: 21px" > 使用标准HTML来进

基于ASP.NET+easyUI框架实现图片上传功能(表单)_实用技巧

基于ASP.Net +easyUI框架上传图片,实现图片上传,提交表单: <body> <link href="../../Easyui/themes/easyui.css" rel="stylesheet" type="text/css" /> <script charset="utf-8" src="../../Easyui/jquery.easyui.min.js" ty

ASP.NET 常用 文件上传方法第1/2页_实用技巧

本文主要内容包括: 1.如何解决文件上传大小的限制 2.以文件形式保存到服务器 3.转换成二进制字节流保存到数据库以及下载方法 4.上传Internet上的资源 第一部分: 首先我们来说一下如何解决ASP.NET中的文件上传大小限制的问题,我们知道在默认情况下ASP.NET的文件上传大小限制为2M,一般情况下,我们可以采用更改WEB.Config文件来自定义最大文件大小,如下: 复制代码 代码如下: <httpRuntime executionTimeout="300" maxR

asp.net上传文件到数据库的解决方案_实用技巧

现在,我们来看存放文件的数据库表结构,这里,我们给出建立表的标准SQL语句: CREATE TABLE tblBooksUpload ( DocID int NOT NULL IDENTITY Primary Key , DocTitle varchar (200) , Doc image, DocType varchar (50) , Entrydate datetime Default GetDate() ) 以上的语句中,我们看到数据表tblBooksUpload包含五个字段: ·字段Do

ASP.NET解决上传大文件问题的方法_实用技巧

上传文件的控件为:FileUpload Asp.Net对上传文件大小有限制.默认情况下用户只能上传4MB大小的文件,这会给用户带来不便.所以如果要上传40MB大小的文件.只能修改配置文件 关键代码如下 复制代码 代码如下:  protected void btnSend_Click(object sender, EventArgs e)     {         try         {             //上传文件的思路:             //获取上传文件的名称,此时为一个

Javascript 直接调用服务器C#代码 ASP.NET Ajax实例_实用技巧

在MS Ajax中,JS与C#交互的一种方式就是调用WebService,该WebService可以ASMX的也可以是WCF的,不论哪种方式,系统都会自动为开发者生成代理的JS类.实现方法如下: 1.        建立一个网站,并在其中添加一个WCF服务(这里一定要选择Ajax-Enabled WCF Service),如下图所示: 2.        IDE会自动为我们生成一个SVC文件,是对外的接口,以及该SVC对应的后台实现类,该类文件会被放在App_Code下,如下图所示: 3.修改该