首页 > 代码库 > asp.net 文件上传
asp.net 文件上传
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //context.Response.Write(context.Request["cheshi1"]); if (context.Request.Files.Count > 0) { for (int i = 0; i < context.Request.Files.Count; i++) { HttpPostedFile file = context.Request.Files[i]; //检查文件类型,只接收jpg,jpeg,bmp格式 if (file.ContentType=="image/jpeg") { //通过字符串哈希码,创建多层目录随机保存路径。 string guid = Guid.NewGuid().ToString(); int ram = guid.GetHashCode(); int dirA = ram & 0xf; //0xf ‘1111‘ 整数15 int dirB = (ram >> 4) & 0xf; //ram 右移4位 int dirC = (ram >> 8) & 0xf; //ram 右移8位 string path = context.Server.MapPath(string.Format("Upload/{0}/{1}/{2}/", dirA.ToString(), dirB.ToString(), dirC.ToString())); System.IO.Directory.CreateDirectory(path); //创建目录。 file.SaveAs(path + guid + "_" + file.FileName); //创建缩略图并保存 Image image = Image.FromStream(file.InputStream); Image smallImg = new Bitmap(200, 200 * image.Height / image.Width); Graphics g = Graphics.FromImage(smallImg); g.DrawImage(image, 0, 0, smallImg.Width, smallImg.Height); smallImg.Save(path + guid + "_small" + file.FileName); context.Response.Write("“" + file.FileName + "”上传成功!\r\n"); } else { context.Response.Write("上传文件格式错误! is:" + file.FileName + "\r\n"); } } } else { context.Response.Write("上传失败!"); } }
<script type="text/javascript"> window.onload = function () { document.getElementById(‘add‘).onclick = function () { var file1 = document.getElementById(‘file1‘); var ext = /\.[^\.]+$/.exec(file1.value.toLowerCase()); if (ext == ‘.jpeg‘ || ext == ‘.jpg‘ || ext == ‘.bmp‘) { return; } else { alert(‘上传文件格式错误!‘); return false; } } }; </script>
<form id="form1" method="post" action="AddNews.aspx" enctype="application/x-www-form-urlencoded"><input type="file" name="txtImage" id="file1" value="" /><input id="add" type="submit" value="提交" />
asp.net 文件上传
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。