十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇内容介绍了“ASP.NET上传实例介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计制作、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的米易网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
KindEditor是一个不错的网页在线编辑器,可是它只提供了asp,php,jsp上传的类,没有提供ASP.NET上传的类。
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]);
}
}
}
}
///
/// 提示关闭层
///
/// name="MsgStr">
private void Alert(string MsgStr)
{
Response.Write("");
Response.Write("");
Response.Write("
error"); Response.Write(" http-equiv=\"content-type\" content=\"text/html;
charset=utf-8\">");Response.Write("");
Response.Write("");
Response.Write("");
Response.Write("");
Response.Write("");
}
///
/// 检测文件类型
///
/// name="ExtStr">
/// name="fileExt">
///
private bool CheckExt(string[] ExtStr,string fileExt)
{
for (int i = 0; i < ExtStr.Length; i++)
{
if (ExtStr[i] != fileExt)
{
return true;
}
}
return false;
}
///
/// 返回图片
///
/// name="FileUrl">
/// name="FileWidth">
/// name="FileHeight">
/// name="FileBorder">
/// name="FileTitle">
/// name="FileAlign">
/// name="FileHspace">
/// name="FileVspace">
private void ReturnImg( string FileUrl,string FileWidth,string FileHeight,
string FileBorder,string FileTitle,string FileAlign,string FileHspace,string FileVspace){
Response.Write("");
Response.Write("");
Response.Write("
上传成功"); Response.Write(" http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
Response.Write("");
Response.Write("");
Response.Write("");
Response.Write("");
Response.Write("");
}
}
“ASP.NET上传实例介绍”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!