码迷,mamicode.com
首页 > Web开发 > 详细

asp.net 文件上传

时间:2014-10-22 21:35:03      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   java   for   

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 文件上传

标签:style   blog   http   color   io   os   ar   java   for   

原文地址:http://www.cnblogs.com/han1982/p/4044210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!