主题: KindEditor增加asp.net版图片上传源码(与大家分享)
作者: 青春一度, 发布日期: 2007-02-05 17:25:55, 浏览数: 12429
//UpLoadNet.aspx.cs文件

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//=======================================
// 印像新闻系统.NET VER 1.0c
// 发布于2006.7.30
// by 留印 QQ:14339095
// http://www.115000.com.cn
//=======================================

// 易网设计|易点通WEB源
// www.115000.com.cn
// ╔------------------------------------------------╗
// ┆软件下载 源码下载 各种教程 二手信息 企业黄页┆
// ┆交流论坛 免费服务 技术咨询 产品推广 免费代码┆
// ┆网页设计 虚拟主机 域名注册 电脑培训 网站推广┆
// ╚------------------------------------------------╝
//
// 留印,为您提供网站服务......
//
// 自助友情链接:
// http://link.115000.com.cn/
// 易点通WEB源:(最简单-最快捷-让您享受到最实用的WEB源代码)
// http://liuy.115000.com.cn/
// http://www.115000.com.cn/
// QQ群组:4052669
// ASP,.NET,PHP技术讨论欢迎各界有缘朋友加入。


public partial class upload_cgi : System.Web.UI.Page
{
// protected System.Web.UI.WebControls.Button UploadButton;

// protected System.Web.UI.WebControls.Label strStatus;

protected void Page_Load(object sender, EventArgs e)
{

/// 在此处放置用户代码以初始化页面

if (!this.IsPostBack) this.SaveImages();

}
private Boolean SaveImages()
{
liuyinconn yldate = new liuyinconn();//时间转换

string filepath = "liuyin_UploadFile/";//文件目录

string filepathdate = "" + yldate.yldate(System.DateTime.Now, "y-m-d") + "";//当天目录

///'遍历File表单元素

HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息

// System.Text.StringBuilder strMsg = new System.Text.StringBuilder();

//strMsg.Append("上传的文件分别是:<hr color=red>");

try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字

HttpPostedFile postedFile = files[iFile];
string FileName;
FileName = System.IO.Path.GetFileName(postedFile.FileName);//获取初始文件名

if (FileName != "")
{
string fileExtension = System.IO.Path.GetExtension(FileName);//获取文件扩展名

//int i = FileName.LastIndexOf("."); //取得文件名中最后一个"."的索引

//string fileExtension = FileName.Substring(i); //获取文件扩展名


if (fileExtension != ".gif" && fileExtension != ".jpg" && fileExtension != ".jpeg" && fileExtension != ".bmp" && fileExtension != ".png")
{
Response.Write("<div style="font-size:12px;color:#333333;padding-top:0px;">上传失败![ <a style="CURSOR: pointer" onclick=history.go(-1) ><font color=red>重新上传</font></a> ]</div>");
Response.End();
}
string photoName2 = "" + yldate.yldate(System.DateTime.Now, "yyyy-m-d-h-m-s-ms") + "_" + iFile + "" + fileExtension; //重新为文件命名,时间毫秒部分+文件大小+扩展名

System.IO.Directory.CreateDirectory(@Server.MapPath("../" + filepath + "" + filepathdate + ""));//生成文件完整目录

///'可根据扩展名字的不同保存到不同的文件夹

///注意:可能要修改你的文件夹的匿名写入权限。

postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../" + filepath + "" + filepathdate + "/") + photoName2);

//写入到编辑器

//Response.Write("<A href="" + filepath + "" + filepathdate + "/" + photoName2 + "" target=_blank><img onmousewheel="return imgzoom_b_s(this);" alt=CTRL+鼠标滚轮放大或缩小 src="" + filepath + "" + filepathdate + "/" + photoName2 + "" onload="return imgzoom(this,480)" border=0></a>");


Response.Write("<script type="text/javascript">parent.document.KindEditorForm.document.body.innerHTML+='<a href="" + filepath + "" + filepathdate + "/" + photoName2 + "" title=点击放大 target=_blank><img onmousewheel="return imgzoom_b_s(this);" alt=CTRL+鼠标滚轮放大或缩小 src="" + filepath + "" + filepathdate + "/" + photoName2 + "" onload="return imgzoom(this,480)" border=0></a><br><br>';</script>");
//strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");

//strMsg.Append("上传后文件地址:" + filepath + "" + filepathdate + "/" + photoName2 + "<br>");

//strMsg.Append("上传文件的文件名:" + FileName + "<br>");

//strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");

}
else
{
//strStatus.Text = "上传失败!";

Response.Write("<div style="font-size:12px;color:#333333;padding-top:0px;">上传失败![ <a style="CURSOR: pointer" onclick=history.go(-1) ><font color=red>重新上传</font></a> ]</div>");
Response.End();
return false;
}
}
//显示是否成功!

Response.Write("<div style="font-size:12px;color:#333333;padding-top:0px;">文件已经成功上传![ <a style="CURSOR: pointer" onclick=history.go(-1) ><font color=red>继续上传</font></a> ]</div>");

//strStatus.Text = strMsg.ToString();

Response.End();
return true;
}
catch (System.Exception Ex)
{
//strStatus.Text = "上传失败!";

//Response.Write("<div style="font-size:12px;color:#333333;padding-top:0px;">上传失败![ <a style="CURSOR: pointer" onclick=history.go(-1) ><font color=red>重新上传</font></a> ]</div>");

Response.End();
return false;
}
}


}



//注:(以下为每天建一个目录和按时间给文件重命名源码和使用方法)
//类名可以自己定义liuyinconn

//使用方法
liuyinconn yldate = new liuyinconn();//时间转换
yldate.yldate(System.DateTime.Now, "y-m-d")//每天建一个目录
yldate.yldate(System.DateTime.Now, "yyyy-m-d-h-m-s-ms") //文件重命名


//时间转换源码
public string yldate(DateTime DateTime, string ShowType)
{
//自定义时间函数
string Date = "";
//DateTime currentTime = System.DateTime.Now; //获取系统当前时间
DateTime currentTime = DateTime; //获取系统当前时间
// 1.2 取当前年
string yy = currentTime.Year.ToString();
//1.3 取当前月
string mm = currentTime.Month.ToString();
//1.4 取当前日
string dd = currentTime.Day.ToString();
//1.5 取当前时
string hh = currentTime.Hour.ToString();
//1.6 取当前分
string m = currentTime.Minute.ToString();
//1.7 取当前秒
string s = currentTime.Second.ToString();
//1.8 取当前毫秒
string ms = currentTime.Millisecond.ToString();
switch (ShowType)
{
case "y-m-d":
Date = "" + yy + "-" + mm + "-" + dd + "";
break;
case "h:m":
Date = "" + hh + "-" + m + "";
break;
case "yy-mm-dd":
Date = "" + yy + "年" + mm + "月" + dd + "日";
break;
case "hh:mm":
Date = "" + hh + "时" + m + "分";
break;
case "yyyy-m-d-h-m-s-ms"://用于上传文件重命名
Date = "" + yy + "" + mm + "" + dd + "" + hh + "" + m + "" + s + "" + ms + "";
break;
}
return Date;

}








//////////////////////////////////////UpLoadNet.aspx文件为:


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

<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>
作者: 青春一度, 发布日期: 2007-02-05 17:27:15
TO:Roddy


上传图片的属性和批量上传没弄。你有时间可以修改。

你的上传源码我可以再研究研究!哈哈。谢谢你的编辑器!
回复
发表新帖 发表回复