熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

KindEditor圖片上傳Asp.net代碼

2022-06-13   來源: .NET編程 
    KindEditor是一個不錯的網頁在線編輯器可是它只提供了aspphpjsp上傳的類沒有提供上傳的類

  using System;
using SystemGlobalization;
using SystemCollections;
using SystemConfiguration;
using SystemData;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIHtmlControls;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;

  public partial class Jscript_KindEditor_upload_cgi_upload : SystemWebUIPage
{
protected void Page_Load(object sender EventArgs e)
{
//文件保存目錄路徑
string SavePath = /Upload_Images/;
//文件保存目錄URL
string SaveUrl = /Upload_Images/;
//上傳圖片類型
string[] ExtStr=new string[];
ExtStr[] = gif;
ExtStr[] = jpg;
ExtStr[] = png;
ExtStr[] = bmp;
//圖片的最大大小
int MaxSize = ;
//錯誤提示
string[] MsgStr = new string[];
MsgStr[] = 上傳文件大小超過限制;
MsgStr[] = 上傳文件擴展名是不允許的擴展名;
MsgStr[] = 上傳文件失敗\\n請重試;

  string imgWidth = RequestForm[imgWidth];
string imgHeight = RequestForm[imgHeight];
string imgBorder = RequestForm[imgBorder];
string imgTitle = RequestForm[imgTitle];
string imgAlign = RequestForm[imgAlign];
string imgHspace = RequestForm[imgHspace];
string imgVspace = RequestForm[imgVspace];

  HttpPostedFile imgFile = HttpContextCurrentRequestFiles[imgFile];
//獲得文件名
string FileName = SystemIOPathGetFileName(imgFileFileName);

  if (FileName != )
{
if (imgFileContentLength > MaxSize)
{
Alert(MsgStr[]);
}
else
{
string fileExtension = SystemIOPathGetExtension(FileName)ToLower();
if (CheckExt(ExtStr fileExtension))
{
//重新為文件命名時間毫秒部分+擴展名
string imgReName = + DateTimeNowToString(yyyyMMddHHmmssffff DateTimeFormatInfoInvariantInfo) + + fileExtension;
//文件夾名
string imgFolderName=DateTimeNowToString(yyyyMMddDateTimeFormatInfoInvariantInfo);

  try
{

  if (!SystemIODirectoryExists(@ServerMapPath( + SavePath + +imgFolderName + )))
{
//生成文件完整目錄
SystemIODirectoryCreateDirectory(@ServerMapPath( + SavePath + +imgFolderName + ));
}

  imgFileSaveAs(@ServerMapPath( + SavePath + + imgFolderName + /)+imgReName);

  }
catch
{
Alert(MsgStr[]);
}
string imgUrl = SaveUrl + imgFolderName + / + imgReName;
ReturnImg(imgUrl imgWidth imgHeight imgBorder imgTitle imgAlign imgHspace imgVspace);

  }
else
{
Alert(MsgStr[]);
}
}
}

  }
/// <summary>
/// 提示關閉層
/// </summary>
/// <param name=MsgStr></param>
private void Alert(string MsgStr)
{

  ResponseWrite(<html>);
ResponseWrite(<head>);
ResponseWrite(<title>error</title>);
ResponseWrite(<meta httpequiv=\contenttype\ content=\text/html; charset=utf\>);
ResponseWrite(</head>);
ResponseWrite(<body>);
ResponseWrite(<script type=\text/javascript\>alert(\ + MsgStr + \);parentKindDisableMenu();parentKindReloadIframe();</script>);
ResponseWrite(</body>);
ResponseWrite(</html>);
}
/// <summary>
/// 檢測文件類型
/// </summary>
/// <param name=ExtStr></param>
/// <param name=fileExt></param>
/// <returns></returns>
private bool CheckExt(string[] ExtStrstring fileExt)
{
for (int i = ; i < ExtStrLength; 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 FileUrlstring FileWidthstring FileHeightstring FileBorderstring FileTitlestring FileAlignstring FileHspacestring FileVspace)
{
ResponseWrite(<html>);
ResponseWrite(<head>);
ResponseWrite(<title>上傳成功</title>);
ResponseWrite(<meta httpequiv=\contenttype\ content=\text/html; charset=utf\>);
ResponseWrite(</head>);
ResponseWrite(<body>);
ResponseWrite(<script type=\text/javascript\>parentKindInsertImage(\ + FileUrl +\\ + FileWidth + \\ + FileHeight + \\ + FileBorder + \\ + FileTitle + \\ + FileAlign + \\ + FileHspace + \\ + FileVspace + \);</script>);
ResponseWrite(</body>);
ResponseWrite(</html>);
}
}


From:http://tw.wingwit.com/Article/program/net/201311/13494.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.