ImageUpload

using System;
using System.IO;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Drawing;

/// <summary>
/// 文件类型
/// </summary>
public enum FileExtension
{
    JPG = 255216,
    GIF = 7173,
    BMP = 6677,
    PNG = 13780,
    RAR = 8297,
    jpg = 255216,
    exe = 7790,
    xml = 6063,
    html = 6033,
    aspx = 239187,
    cs = 117115,
    js = 119105,
    txt = 210187,
    sql = 255254
}

/// <summary>
/// 图片检测类
/// </summary>
public static class FileValidation
{
    #region 上传图片检测类
    /// <summary>
    /// 是否允许
    /// </summary>
    public static bool IsAllowedExtension(HttpPostedFile oFile, FileExtension[] fileEx)
    {
        int fileLen = oFile.ContentLength;
        byte[] imgArray = new byte[fileLen];
        oFile.InputStream.Read(imgArray, 0, fileLen);
        MemoryStream ms = new MemoryStream(imgArray);
        System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
        string fileclass = "";
        byte buffer;
        try
        {
            buffer = br.ReadByte();
            fileclass = buffer.ToString();
            buffer = br.ReadByte();
            fileclass += buffer.ToString();
        }
        catch { }
        br.Close();
        ms.Close();
        foreach (FileExtension fe in fileEx)
        {
            if (Int32.Parse(fileclass) == (int)fe) return true;
        }
        return false;
    }

/// <summary>
    /// 上传前的图片是否可靠
    /// </summary>
    public static bool IsSecureUploadPhoto(HttpPostedFile oFile)
    {
        bool isPhoto = false;
        string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower();
        string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (fileExtension == allowedExtensions[i])
            {
                isPhoto = true;
                break;
            }
        }
        if (!isPhoto)
        {
            return true;
        }
        FileExtension[] fe = {FileExtension.BMP,FileExtension.GIF,FileExtension.JPG,FileExtension.PNG};

if (IsAllowedExtension(oFile, fe))
            return true;
        else
            return false;
    }

/// <summary>
    /// 上传后的图片是否安全
    /// </summary>
    /// <param name="photoFile">物理地址</param>
    public static bool IsSecureUpfilePhoto(string photoFile)
    {
        bool isPhoto = false;
        string Img = "Yes";
        string fileExtension = System.IO.Path.GetExtension(photoFile).ToLower();
        string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (fileExtension == allowedExtensions[i])
            {
                isPhoto = true;
                break;
            }
        }

if (!isPhoto)
        {
            return true;
        }
        StreamReader sr = new StreamReader(photoFile, System.Text.Encoding.Default);
        string strContent = sr.ReadToEnd();
        sr.Close();
        string str = "request|<script|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=";
        foreach (string s in str.Split(‘|‘))
        {
            if (strContent.ToLower().IndexOf(s) != -1)
            {
                File.Delete(photoFile);
                Img = "No";
                break;
            }
        }
        return (Img == "Yes");
    }
    #endregion
}

/// <summary>
/// 图片上传类
/// </summary>
//----------------调用-------------------
//imageUpload iu = new imageUpload();
//iu.AddText = "";
//iu.CopyIamgePath = "";
//iu.DrawString_x = ;
//iu.DrawString_y = ;
//iu.DrawStyle = ;
//iu.Font = "";
//iu.FontSize = ;
//iu.FormFile = File1;
//iu.IsCreateImg =;
//iu.IsDraw = ;
//iu.OutFileName = "";
//iu.OutThumbFileName = "";
//iu.SavePath = @"~/image/";
//iu.SaveType = ;
//iu.sHeight  = ;
//iu.sWidth   = ;
//iu.Upload();
//--------------------------------------
public class ImageUpload
{
    #region 私有成员
    private int _Error = 0;//返回上传状态。
    private int _MaxSize = 1024 * 1024;//最大单个上传文件 (默认)
    private string _FileType = "jpg;gif;bmp;png";//所支持的上传类型用"/"隔开
    private string _SavePath = System.Web.HttpContext.Current.Server.MapPath(".") + "\\";//保存文件的实际路径
    private int _SaveType = 0;//上传文件的类型,0代表自动生成文件名
    private HtmlInputFile _FormFile;//上传控件。
    private string _InFileName = "";//非自动生成文件名设置。
    private string _OutFileName = "";//输出文件名。
    private bool _IsCreateImg = true;//是否生成缩略图。
    private bool _Iss = false;//是否有缩略图生成.
    private int _Height = 0;//获取上传图片的高度
    private int _Width = 0;//获取上传图片的宽度
    private int _sHeight = 120;//设置生成缩略图的高度
    private int _sWidth = 120;//设置生成缩略图的宽度
    private bool _IsDraw = false;//设置是否加水印
    private int _DrawStyle = 0;//设置加水印的方式0:文字水印模式,1:图片水印模式,2:不加
    private int _DrawString_x = 10;//绘制文本的X坐标(左上角)
    private int _DrawString_y = 10;//绘制文本的Y坐标(左上角)
    private string _AddText = "GlobalNatureCrafts";//设置水印内容
    private string _Font = "宋体";//设置水印字体
    private int _FontSize = 12;//设置水印字大小
    private int _FileSize = 0;//获取已经上传文件的大小
    private string _CopyIamgePath = System.Web.HttpContext.Current.Server.MapPath(".") + "/images/5dm_new.jpg";//图片水印模式下的覆盖图片的实际地址
    #endregion

#region 公有属性
    /// <summary>
    /// Error返回值
    /// 1、没有上传的文件
    /// 2、类型不允许
    /// 3、大小超限
    /// 4、未知错误
    /// 0、上传成功。
    /// </summary>
    public int Error
    {
        get { return _Error; }
    }

/// <summary>
    /// 最大单个上传文件
    /// </summary>
    public int MaxSize
    {
        set { _MaxSize = value; }
    }

/// <summary>
    /// 所支持的上传类型用";"隔开
    /// </summary>
    public string FileType
    {
        set { _FileType = value; }
    }

/// <summary>
    /// 保存文件的实际路径
    /// </summary>
    public string SavePath
    {
        set { _SavePath = System.Web.HttpContext.Current.Server.MapPath(value); }
        get { return _SavePath; }
    }

/// <summary>
    /// 上传文件的类型,0代表自动生成文件名
    /// </summary>
    public int SaveType
    {
        set { _SaveType = value; }
    }

/// <summary>
    /// 上传控件
    /// </summary>
    public HtmlInputFile FormFile
    {
        set { _FormFile = value; }
    }

/// <summary>
    /// 非自动生成文件名设置。
    /// </summary>
    public string InFileName
    {
        set { _InFileName = value; }
    }

/// <summary>
    /// 输出文件名
    /// </summary>
    public string OutFileName
    {
        get { return _OutFileName; }
        set { _OutFileName = value; }
    }

/// <summary>
    /// 输出的缩略图文件名
    /// </summary>
    public string OutThumbFileName
    {
        get;
        set;
    }

/// <summary>
    /// 是否有缩略图生成.
    /// </summary>
    public bool Iss
    {
        get { return _Iss; }
    }

/// <summary>
    /// 获取上传图片的宽度
    /// </summary>
    public int Width
    {
        get { return _Width; }
    }

/// <summary>
    /// 获取上传图片的高度
    /// </summary>
    public int Height
    {
        get { return _Height; }
    }

/// <summary>
    /// 设置缩略图的宽度
    /// </summary>
    public int sWidth
    {
        get { return _sWidth; }
        set { _sWidth = value; }
    }

/// <summary>
    /// 设置缩略图的高度
    /// </summary>
    public int sHeight
    {
        get { return _sHeight; }
        set { _sHeight = value; }
    }

/// <summary>
    /// 是否生成缩略图
    /// </summary>
    public bool IsCreateImg
    {
        get { return _IsCreateImg; }
        set { _IsCreateImg = value; }
    }

/// <summary>
    /// 是否加水印
    /// </summary>
    public bool IsDraw
    {
        get { return _IsDraw; }
        set { _IsDraw = value; }
    }

/// <summary>
    /// 设置加水印的方式
    /// 0:文字水印模式
    /// 1:图片水印模式
    /// 2:不加
    /// </summary>
    public int DrawStyle
    {
        get { return _DrawStyle; }
        set { _DrawStyle = value; }
    }

/// <summary>
    /// 绘制文本的X坐标(左上角)
    /// </summary>
    public int DrawString_x
    {
        get { return _DrawString_x; }
        set { _DrawString_x = value; }
    }

/// <summary>
    /// 绘制文本的Y坐标(左上角)
    /// </summary>
    public int DrawString_y
    {
        get { return _DrawString_y; }
        set { _DrawString_y = value; }
    }

/// <summary>
    /// 设置文字水印内容
    /// </summary>
    public string AddText
    {
        get { return _AddText; }
        set { _AddText = value; }
    }

/// <summary>
    /// 设置文字水印字体
    /// </summary>
    public string Font
    {
        get { return _Font; }
        set { _Font = value; }
    }

/// <summary>
    /// 设置文字水印字的大小
    /// </summary>
    public int FontSize
    {
        get { return _FontSize; }
        set { _FontSize = value; }
    }

/// <summary>
    /// 文件大小
    /// </summary>
    public int FileSize
    {
        get { return _FileSize; }
        set { _FileSize = value; }
    }

/// <summary>
    /// 图片水印模式下的覆盖图片的实际地址
    /// </summary>
    public string CopyIamgePath
    {
        set { _CopyIamgePath = System.Web.HttpContext.Current.Server.MapPath(value); }
    }

#endregion

#region 私有方法
    /// <summary>
    /// 获取文件的后缀名
    /// </summary>
    private string GetExt(string path)
    {
        return Path.GetExtension(path);
    }

/// <summary>
    /// 获取输出文件的文件名
    /// </summary>
    private string FileName(string Ext)
    {
        if (_SaveType == 0 || _InFileName.Trim() == "")
            return DateTime.Now.ToString("yyyyMMddHHmmssfff") + Ext;
        else
            return _InFileName;
    }

/// <summary>
    /// 检查上传的文件的类型,是否允许上传。
    /// </summary>
    private bool IsUpload(string Ext)
    {
        Ext = Ext.Replace(".", "");
        bool b = false;
        string[] arrFileType = _FileType.Split(‘;‘);
        foreach (string str in arrFileType)
        {
            if (str.ToLower() == Ext.ToLower())
            {
                b = true;
                break;
            }
        }
        return b;
    }
    #endregion

#region 上传图片
    public void Upload()
    {
        HttpPostedFile hpFile = _FormFile.PostedFile;
        if (hpFile == null || hpFile.FileName.Trim() == "")
        {
            _Error = 1;
            return;
        }
        string Ext = GetExt(hpFile.FileName);
        if (!IsUpload(Ext))
        {
            _Error = 2;
            return;
        }
        int iLen = hpFile.ContentLength;
        if (iLen > _MaxSize)
        {
            _Error = 3;
            return;
        }
        try
        {
            if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
            byte[] bData = new byte[iLen];
            hpFile.InputStream.Read(bData, 0, iLen);
            string FName;
            FName = FileName(Ext);
            string TempFile = "";
            if (_IsDraw)
            {
                TempFile = FName.Split(‘.‘).GetValue(0).ToString() + "_temp." + FName.Split(‘.‘).GetValue(1).ToString();
            }
            else
            {
                TempFile = FName;
            }
            FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
            newFile.Write(bData, 0, bData.Length);
            newFile.Flush();
            int _FileSizeTemp = hpFile.ContentLength;

string ImageFilePath = _SavePath + FName;
            if (_IsDraw)
            {
                if (_DrawStyle == 0)
                {
                    System.Drawing.Image Img1 = System.Drawing.Image.FromStream(newFile);
                    Graphics g = Graphics.FromImage(Img1);
                    g.DrawImage(Img1, 100, 100, Img1.Width, Img1.Height);
                    Font f = new Font(_Font, _FontSize);
                    Brush b = new SolidBrush(Color.Red);
                    string addtext = _AddText;
                    g.DrawString(addtext, f, b, _DrawString_x, _DrawString_y);
                    g.Dispose();
                    Img1.Save(ImageFilePath);
                    Img1.Dispose();
                }
                else
                {
                    System.Drawing.Image image = System.Drawing.Image.FromStream(newFile);
                    System.Drawing.Image copyImage = System.Drawing.Image.FromFile(_CopyIamgePath);
                    Graphics g = Graphics.FromImage(image);
                    g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width - 5, image.Height - copyImage.Height - 5, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                    g.Dispose();
                    image.Save(ImageFilePath);
                    image.Dispose();
                }
            }

//获取图片的高度和宽度
            System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
            _Width = Img.Width;
            _Height = Img.Height;

//生成缩略图部分
            if (_IsCreateImg)
            {
                #region 缩略图大小只设置了最大范围,并不是实际大小
                float realbili = (float)_Width / (float)_Height;
                float wishbili = (float)_sWidth / (float)_sHeight;

//实际图比缩略图最大尺寸更宽矮,以宽为准
                if (realbili > wishbili)
                {
                    _sHeight = (int)((float)_sWidth / realbili);
                }
                //实际图比缩略图最大尺寸更高长,以高为准
                else
                {
                    _sWidth = (int)((float)_sHeight * realbili);
                }
                #endregion

this.OutThumbFileName = FName.Split(‘.‘).GetValue(0).ToString() + "_s." + FName.Split(‘.‘).GetValue(1).ToString();
                string ImgFilePath = _SavePath + this.OutThumbFileName;

System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
                newImg.Save(ImgFilePath);
                newImg.Dispose();
                _Iss = true;
            }

if (_IsDraw)
            {
                if (File.Exists(_SavePath + FName.Split(‘.‘).GetValue(0).ToString() + "_temp." + FName.Split(‘.‘).GetValue(1).ToString()))
                {
                    newFile.Dispose();
                    File.Delete(_SavePath + FName.Split(‘.‘).GetValue(0).ToString() + "_temp." + FName.Split(‘.‘).GetValue(1).ToString());
                }
            }
            newFile.Close();
            newFile.Dispose();
            _OutFileName = FName;
            _FileSize = _FileSizeTemp;
            _Error = 0;
            return;
        }
        catch (Exception ex)
        {
            _Error = 4;
            return;
        }
    }
    #endregion
}

时间: 2024-10-10 08:25:07

ImageUpload的相关文章

fileUpload, imageUpload solutions

https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript https://developer.mozilla.org/en

C#通过form表单上传图片

1 <head> 2 <title>图片上传</title> 3 <script src="../Scripts/jquery-1.7.1.js"></script> 4 <script type="text/javascript"> 5 $(function () { 6 $(":file").change(function(){ 7 // alert("chan

HTML5图片预览

作者:Snandy 两种方式实现 URL FileReader 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 <!DOCTYPE HTML> <html>     <head>     <meta charset="utf

PeopleSoft Rich Text Boxes上定制Tool Bars

在使用PT8.50或在8.51时,你可能遇到过Rich-text编辑框.该插件使你能够格式化文本,添加颜色.链接.图片等等.下面是效果图: 如果页面中只有这么一个字段,该文本框就会有足够的空间来容纳其中的tools bars了,但是通常页面中会有许多字段, 因而留给该表示该文本框的字段的所需的空间就少了,于是需要根据业务需求对文本框中的工具条目进行瘦身或者适当的扩展. 简单的实现方式如下: 在Long Edit Box的Page Field 属性上有“Options”选项,勾选“Enable R

Asp.Net 自定义控件实现图片的上传,浏览,删除功能

4月的时候公司比较闲,就想着自己做点东西,其实主要是为了更加熟悉.Net,毕竟接触的时间不长,趁着有时间想提高提高.不过当我做到图片上传这个功能的时候,就有些停滞不前了,连续写了两天也达不到自己想要的标准.后来公司来活,然后就没有然后了,然而做事总不能半途而废吧~时隔一个多月,趁着这个周末,我再次拾起了这个项目,而首要工作就是攻破这个图片上传控件. 下面说说我的标准是什么子的吧~ 1.最多可以上传三张图片,超过三张有提示. 2.点击图片小图,有图片放大功能,再次点击,图片恢复原来尺寸. 3.在图

上传文件通用写法

1.html页面 注意:form的enctype="multipart/form-data" <form method="post" action="FileUp.ashx" enctype="multipart/form-data" > <input type="file" name="fileUp" /> <input type="submit

部分安卓手机微信浏览器中使用XMLHttpRequest 2上传图片显示字节数为0的解决办法

前端JS中使用XMLHttpRequest 2上传图片到服务器,PC端和大部分手机上都正常,但在少部分安卓手机上上传失败,服务器上查看图片,显示字节数为0.下面是上传图片的核心代码: HTML <input type="file" id="choose" capture="camera" accept="image/*"> JavaScript var filechooser = document.getEleme

Yii2系列教程六:集成编辑器

上一篇文章我们实现了简单的用户权限管理,至于更先进的RBAC,我后面会单独出一篇文章来说说.在这一篇文章当中,我主要想写的是在Yii2中集成一个编辑器,因为在我们的实际开发当中,一个简单的textarea一般都是不能满足我们的需求的,因为我们需要多种多样的文本样式如标题,表格啦,并且很多时候我们在这些文本当中还需要插入图片和视频.而这些问题目前的最好解决方案就是集成一个编辑器,鉴于大家(这里不是指程序员)都是喜欢所见即所得,所以,这里我主要是演示怎么集成所见即所得的富文本编辑器. Redacto

04asp.net==========改变水印的透明度

public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //1:接收发送过来的文件的数据. HttpPostedFile file =context.Request.Files["Filedata"]; string fileName = Path.GetFileName(file.FileName);//文件名称 string fi