没有合适的资源?快使用搜索试试~ 我知道了~
方法里包括了图片大小限制、图片尺寸、文件内容等等的判断。。。 该案例是mvc下的demo,支持单张图片上传。 public ActionResult Upload() { string imgurl = ; foreach (string key in Request.Files) { //这里只测试上传第一张图片file[0] HttpPostedFileBase file0 = Request.Files[key]; //转换成byte,读取图片MIME类型 Stream st
资源详情
资源评论
资源推荐
C# 最齐全的上传图片方法最齐全的上传图片方法
方法里包括了图片大小限制、图片尺寸、文件内容等等的判断。。。
该案例是该案例是mvc下的下的demo,支持单张图片上传。,支持单张图片上传。
public ActionResult Upload()
{
string imgurl = "";
foreach (string key in Request.Files)
{
//这里只测试上传第一张图片file[0] HttpPostedFileBase file0 = Request.Files[key];
//转换成byte,读取图片MIME类型
Stream stream;
int size = file0.ContentLength / 1024; //文件大小KB
if (size > 1024)
{
return Content(ReturnMsg(Enum_Return.失败, "图片不能超过1M:", null));
}
byte[] fileByte = new byte[2];//contentLength,这里我们只读取文件长度的前两位用于判断就好了,这样速度比较快,剩下的也用不到。
stream = file0.InputStream;
stream.Read(fileByte, 0, 2);//contentLength,还是取前两位
//获取图片宽和高
//System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
//int width = image.Width;
//int height = image.Height;
string fileFlag = "";
if (fileByte != null && fileByte.Length > 0)//图片数据是否为空
{
fileFlag = fileByte[0].ToString() fileByte[1].ToString();
}
string[] fileTypeStr = { "255216", "7173", "6677", "13780" };//对应的图片格式jpg,gif,bmp,png
if (fileTypeStr.Contains(fileFlag))
{
string action = Request["action"];
string path = "/uploads/";
switch (action)
{
case "headimage":
path = "headimage/";
break;
case "blogtype":
path = "blogtype/";
break;
}
string fullpath = path UserInfo.userID "/";
if (!Directory.Exists(Server.MapPath(fullpath)))
{
Directory.CreateDirectory(Server.MapPath(fullpath));
}
Request.Files[key].SaveAs(Server.MapPath(fullpath Request.Files[key].FileName));
imgurl = fullpath Request.Files[key].FileName;
}
else
{
return Content(ReturnMsg(Enum_Return.失败, "图片格式不正确:" fileFlag, null));
}
stream.Close();
}
return Content(ReturnMsg(Enum_Return.成功, "上传成功", imgurl));
}
一般处理程序一般处理程序
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
HttpPostedFile _upfile = context.Request.Files["File"];
if (_upfile.ContentLength < 500000)
{
if (string.IsNullOrEmpty(_upfile.FileName))
{
context.Response.Write("请上传图片");
}
string fileFullname = _upfile.FileName;
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\") 1);
weixin_38653664
- 粉丝: 8
- 资源: 951
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0