//传的是我用户id(perid)
public static string UploadUid(int perid) {
//path为你当前项目下的路径, perimage为存储图片的文件夹
string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "perimage/";
var fileNewName = "";
// 如果目录不存在则要先创建
if (Directory.Exists(path)) {
//string path = "D:\freehost\mxcjobcn\web\perimage/";
//用户提交的数据
//var Data = System.Web.HttpContext.Current.Request.Form;
string filesrc = "";
//获取上传的文件
var httpPostedFile = HttpContext.Current.Request.Files;
if (httpPostedFile != null && httpPostedFile.Count > 0) {
var file = httpPostedFile[0];
string imgType = Path.GetExtension(file.FileName);
//限制文件上传类型
if (imgType.Contains(".jpg") || imgType.Contains(".png") || imgType.Contains(".bmp") || imgType.Contains(".jpeg") ||
imgType.Contains(".gif")) {
//返回给前端一个名字用来搜索图片地址并展示
fileNewName = perid + DateTime.Now.ToString("yyyyMMddHHmmss") + imgType;
filesrc = path + fileNewName;
file.SaveAs(filesrc);
}
}
} else {
string filesrc = "";
//获取上传的文件
var httpPostedFile = HttpContext.Current.Request.Files;
if (httpPostedFile != null && httpPostedFile.Count > 0) {
var file = httpPostedFile[0];
string imgType = Path.GetExtension(file.FileName);
//限制文件上传类型
if (imgType.Contains(".jpg") || imgType.Contains(".png") || imgType.Contains(".bmp") || imgType.Contains(".jpeg") ||
imgType.Contains(".gif")) {
fileNewName = perid + DateTime.Now.ToString("yyyyMMddHHmmss") + imgType;
filesrc = path + fileNewName;
file.SaveAs(filesrc);
}
}
}
return fileNewName;
}