【1】HttpContext获取http请求
HttpContext context = HttpContext.Current;
在业务层需要取得当前页面层的Session,Request,Response,Server时可以通过 Http.Current来获得
参考https://www.cnblogs.com/zhouyunbaosujina/p/3164956.html
【2】 判断当前页面是否接收到了Post请求
context.Request.HttpMethod.ToLower().Equals("post")
【3】当前页面获取到的参数和参数值
参考https://q.cnblogs.com/q/108321/
context.Request.Form.AllKeys
foreach (var key in context.Request.Form.AllKeys)
{
var val=context.Request.Form[key];
}
比如参数名Unit,对应参数值就是
context.Request.Form["Unit"]
【4】获取上传的图片,以文件流的形式传过来
context.Request.Form["base64Code1"]
上传多个文件的情况下,用逗号" , "分割文件(备注,多个文件时上传多个文件流参数时遇到问题,获取不到完整的post请求,即只能获取部分参数,故改成只上传一个文件流参数,然后分割开)
var base64Code = context.Request.Form["base64Code1"].Split(',');
【5】文件路径
Server.MapPath方法作用
MapPath方法用来返回与Web服务器上的指定虚拟路径相对应的物理文件路径。
语法
Server.MapPath(path);
参数
path表示Web服务器上的虚拟路径,如果path值为空,则该方法返回包含当前应用程序的完整物理路径。
注意事项
Server.MapPath()有时在程序调试时会提示“当前上下文中不存在名称“Server””错误,从而不支持函数Server.MapPath()的使用。尽管引用了命名空间“using System.Web;”也是无济于事,此时就需要使用其全名,或者是当前使用Server.MapPath()函数的类继承自System.Web.UI.Page。
参考https://blog.csdn.net/nnn_net/article/details/71080905
(1)配置文件里的路径信息
string PicPath = System.Configuration.ConfigurationManager.AppSettings["PicPath"];
(2)生成新的文件名
string fileExt = "jpg"; //文件扩展名,不含“.”
string newFileName = GetTimeStamp() + "_"+(i+1)+"." + fileExt; //随机生成新的文件名
GetTimeStamp是获取时间戳的方法
(3)文件路径,如果不存在,就去创建
string path = "\\";
string pathY = System.Web.HttpContext.Current.Server.MapPath(path + "UploadImg\\avatars\\yt\\");
if (!Directory.Exists(pathY))
{
Directory.CreateDirectory(pathY);
}
(4)保存图片路径--绝对路径
string newFilePath = System.Web.HttpContext.Current.Server.MapPath(path + "UploadImg\\avatars\\yt\\" + newFileName);
【6】上传图片
string str = base64Code[i];
if (str != null)加上 && str!=""空的判断
Image img = Base64ToImg(str);//添加引用Image--us