net文件夹》imageUp.ashx
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//上传配置
int size = 2; //文件大小限制,单位MB //文件大小限制,单位MB
string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" }; //文件允许格式
//上传图片
Hashtable info = new Hashtable();
Uploader up = new Uploader();
string path = ConfigurationManager.AppSettings["ImagePath"]; //上传服务器路径
info = up.upFile(context, path, filetype, size); //获取上传状态
string title = up.getOtherInfo(context, "pictitle"); //获取图片描述
string oriName = up.getOtherInfo(context, "fileName"); //获取原始文件名
HttpContext.Current.Response.Write("{'url':'" + info["url"] + "','title':'" + title + "','original':'" + oriName + "','state':'" + info["state"] + "'}"); //向浏览器返回数据json数据
}
net文件夹》Uploader.cs
public Hashtable upFile(HttpContext cxt, string pathbase, string[] filetype, int size)
{
pathbase = GetFileName(pathbase);//服务器放置的图片文件夹
string _filename = pathbase.Substring(pathbase.LastIndexOf("\") + 1);
pathbase = pathbase + "/";
uploadpath = pathbase;
try
{
uploadFile = cxt.Request.Files[0];
originalName = uploadFile.FileName;
//目录创建
createFolder();
//格式验证
if (checkType(filetype))
{
//不允许的文件类型
state = "u4e0du5141u8bb8u7684u6587u4ef6u7c7bu578b";
}
//大小验证
if (checkSize(size))
{
//文件大小超出网站限制
state = "u6587u4ef6u5927u5c0fu8d85u51fau7f51u7ad9u9650u5236";
}
//保存图片
if (state == "SUCCESS")
{
filename = NameFormater.Format(cxt.Request["fileNameFormat"], originalName);
var testname = filename;
var ai = 1;
while (File.Exists(uploadpath + testname))
{
testname = Path.GetFileNameWithoutExtension(filename) + "_" + ai++ + Path.GetExtension(filename);
}
uploadFile.SaveAs(uploadpath + testname);
string path = ConfigurationManager.AppSettings["WebImagePath"];
URL = path+_filename+"/" + testname;//文本域图片路径
}
}
catch (Exception)
{
// 未知错误
state = "u672au77e5u9519u8bef";
URL = "";
}
return getUploadInfo();
}