asp.net 文件上传

前台js

    <script type="text/javascript">
        window.onload = function () {
            document.getElementById('add').onclick = function () {
                var file1 = document.getElementById('file1');
                if (file1.value == "") {
                    return;
                }
                var ext = /.[^.]+$/.exec(file1.value.toLowerCase());
                if (ext == '.jpeg' || ext == '.jpg' || ext == '.bmp') {
                    return;
                }
                else {
                    alert('上传文件格式错误!');
                    return false;
                }
            }
        };
    </script>

前台aspx

<form id="form1" action="AddNews.aspx" method="post" enctype="multipart/form-data">
图片:<input type="file" name="imgFile" id="file1" />
<input id="add" type="submit" value="提交" />

后台

            string image;
            string smallImage;
            HttpPostedFile file = Request.Files["imgFile"];  //默认4M大小以内,超出延迟无法跳转。
            UploadPic(file, out image, out smallImage);


private bool UploadPic(HttpPostedFile file, out string image, out string smallImage)
    {
        image = "";
        smallImage = "";
        if (file != null)
        {
            //获取文件扩展名
            string ext = System.IO.Path.GetExtension(file.FileName);
            //上传文件类型判断
            if ((ext == ".jpg" || ext == ".jpeg" || ext == ".bmp" || ext == ".png") && file.ContentType.StartsWith("image"))
            {
                //通过字符串哈希码,创建多层目录随机保存路径。
                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位
                //path 文件目录
                string bigPic = string.Format("Upload/BigPic/{0}/{1}/{2}/", dirA.ToString(), dirB.ToString(), dirC.ToString());
                string smallPic = string.Format("Upload/SmallPic/{0}/{1}/{2}/", dirA.ToString(), dirB.ToString(), dirC.ToString());
                //out参数 设值 保存图片路径
                image = System.IO.Path.Combine(bigPic, guid + "_" + file.FileName);
                smallImage = System.IO.Path.Combine(smallPic, guid + "_small" + file.FileName);
                //创建目录
                System.IO.Directory.CreateDirectory(Server.MapPath(bigPic));
                System.IO.Directory.CreateDirectory(Server.MapPath(smallPic));
                //创建缩略图
                Image img = Image.FromStream(file.InputStream);
                Image smallImg = new Bitmap(100, 100 * img.Height / img.Width);
                Graphics g = Graphics.FromImage(smallImg);
                g.DrawImage(img, 0, 0, smallImg.Width, smallImg.Height);

                //保存文件
                file.SaveAs(System.IO.Path.Combine(Server.MapPath("~/NewsAdmin/"), image));
                smallImg.Save(System.IO.Path.Combine(Server.MapPath("~/NewsAdmin/"), smallImage));
            }
            else
            {
                return false;
            }
        }
        return true;
    }
原文地址:https://www.cnblogs.com/han1982/p/4044210.html