![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Cls_Upfile 的摘要说明
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace AllSheng
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class upload
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region 变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Web.HttpPostedFile postedFile;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected string localFileName;//原文件名(含扩展名)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected string localFileExtension;//原扩展名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected long localFileLength;//原文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected string localFilePath;//原文件路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected string saveFileName;//保存的文件名(含扩展名)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected string saveFileExtension;//保存的扩展名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//protected long saveFileLength;//保存的文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected string saveFilePath;//保存文件的服务器端的完整路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected string saveFileFolderPath;//保存文件的服务器端的文件夹路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string path = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string fileType = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int sizes = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region upload():初始化变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 初始化变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public upload()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
path = @"uploadimages"; //上传路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fileType = "jpg|gif|bmp|jpeg|png|rar|doc";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sizes = 200; //传文件的大小,默认200KB
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region 设置传入的值:Path/Sizes/FileType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 设置上传路径,如:uploadimages
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string Path
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
path = @"" + value + @"";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 设置上传文件大小,单位为KB
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int Sizes
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sizes = value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 设置上传文件的类型,如:jpg|gif|bmp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string FileType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fileType = value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region SaveAs()上传文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string SaveAs(System.Web.HttpFileCollection files)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string myReturn = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int iFile = 0; iFile < files.Count; iFile++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
postedFile = files[iFile];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获得文件的上传的路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
localFilePath = postedFile.FileName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//判断上传文件路径是否为空
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (localFilePath == null || localFilePath == "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//message("您没有上传数据呀,是不是搞错了呀!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region 判断文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获得上传文件的大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
localFileLength = postedFile.ContentLength;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//判断上传文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (localFileLength >= sizes * 1024)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message("上传的图片不能大于" + sizes + "KB");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region 文件夹
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获取保存文件夹路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
saveFileFolderPath = getSaveFileFolderPath(path);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region 文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获得原文件名(含扩展名)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
localFileName = System.IO.Path.GetFileName(postedFile.FileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
saveFileName = DateTime.UtcNow.ToString("yyyy" + "MM" + "dd" + "HH" + "mm" + "ss" + "ffffff");//"yyyy"+"MM"+"dd"+"HH"+"mm"+"ss"+"ffffff"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region 扩展名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获取原文件扩展名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
localFileExtension = getFileExtension(localFileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果为真允许上传,为假则不允许上传
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (localFileExtension == "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message("目前本系统支持的格式为:" + fileType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//得到保存文件的扩展名,可根据需要更改扩展名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
saveFileExtension = localFileExtension;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//得到保存文件的完整路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
saveFilePath = saveFileFolderPath + saveFileName + saveFileExtension;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
postedFile.SaveAs(saveFilePath);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
myReturn = myReturn + ((myReturn == "" || myReturn == null) ? "" : "|") + path.TrimStart(new char[] { '\' }) + saveFileName + saveFileExtension;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//以下对文章的内容进行一些加工
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Web.HttpContext.Current.Response.Write("<script>parent.Article_Content___Frame.FCK.EditorDocument.body.innerHTML+='<img src=" + saveFileName + saveFileExtension + " "+" border=0 />'</SCRIPT>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//异常
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message("出现未知错误!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
myReturn = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return myReturn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region getSaveFileFolderPath(...):获得保存的文件夹的物理路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 获得保存的文件夹的物理路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 返回保存的文件夹的物理路径,若为null则表示出错
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="format">保存的文件夹路径 或者 格式化方式创建保存文件的文件夹,如按日期"yyyy"+"MM"+"dd":20060511</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <returns>保存的文件夹的物理路径,若为null则表示出错</returns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string getSaveFileFolderPath(string format)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string mySaveFolder = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string folderPath = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//以当前时间创建文件夹,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//!!!!!!!!!!!!以后用正则表达式替换下面的验证语句!!!!!!!!!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (format.IndexOf("yyyy") > -1 || format.IndexOf("MM") > -1 || format.IndexOf("dd") > -1 || format.IndexOf("hh") > -1 || format.IndexOf("mm") > -1 || format.IndexOf("ss") > -1 || format.IndexOf("ff") > -1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//以通用标准时间创建文件夹的名字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
folderPath = DateTime.UtcNow.ToString(format);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mySaveFolder = System.Web.HttpContext.Current.Server.MapPath(".") + @"" + folderPath + @"";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mySaveFolder = System.Web.HttpContext.Current.Server.MapPath(".") + format;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(mySaveFolder);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//判断文件夹否存在,不存在则创建
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!dir.Exists)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dir.Create();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message("获取保存路径出错");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mySaveFolder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region getFileExtension(...):获取原文件的扩展名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 获取原文件的扩展名,返回原文件的扩展名(localFileExtension),该函数用到外部变量fileType,即允许的文件扩展名.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="myFileName">原文件名</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <returns>原文件的扩展名(localFileExtension);若返回为null,表明文件无后缀名;若返回为"",则表明扩展名为非法.</returns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string getFileExtension(string myFileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string myFileExtension = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获得文件扩展名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
myFileExtension = System.IO.Path.GetExtension(myFileName);//若为null,表明文件无后缀名;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//分解允许上传文件的格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (myFileExtension != "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{myFileExtension = myFileExtension.ToLower();//转化为小写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string[] temp = fileType.Split('|');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置上传的文件是否是允许的格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool flag = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//判断上传的文件是否是允许的格式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (string data in temp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (("." + data) == myFileExtension)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!flag)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
myFileExtension = "";//不能设置成null,因为null表明文件无后缀名;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return myFileExtension;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
#region message(...):弹出消息框
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 弹出消息框,显示内容(msg),点击"确定"后页面跳转到该路径(url)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="msg">显示内容</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <param name="url">跳转路径</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void message(string msg, string url)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');window.location='" + url + "'</script>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 弹出消息框,显示内容(msg),无跳转
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <param name="msg">显示内容</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void message(string msg)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');</script>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}