文件上传

ASPX:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FileUP.ascx.cs" Inherits="AdminHg_FileUP" %>
<asp:FileUpload ID="FU1" runat="server" Width="120px" />
<asp:HyperLink ID="HP1" runat="server" Visible="False">查看</asp:HyperLink>
<asp:Button ID="Button3" runat="server" CausesValidation="False" OnClick="Button3_Click" Text="上传" Width="32px" />
<asp:TextBox ID="InputValue" runat="server" Visible="False" Width="80px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" CausesValidation="False" OnClick="Button1_Click" Text="直接输入" Width="60px" />
<asp:Button ID="Button2" runat="server" CausesValidation="False" Text="确定" Width="32px" OnClick="Button2_Click" Visible="False" />
<asp:Button ID="Button4" runat="server" CausesValidation="False" OnClick="Button4_Click" Text="重新上传" Visible="False" />

ASPX.CS:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.ComponentModel;

public partial class AdminHg_FileUP : System.Web.UI.UserControl
{
    private string savepath,filetype="jpg|gif|jpeg|bmp|png";
    private int filesize = 200;
    private bool newname = true;
    private bool smallimg = false;
    private string filename="";
    private int smallimgwidth = 100;
    private int smallimgheight = 100;
    private SY_Type sytype=SY_Type.NoSY;
    private string sybody;

    public enum SY_Type {
        NoSY,
        ImgSY,
        WordSY
    }

    /// <summary>
    /// 保存路径
    /// </summary>
    public string SavePath {
        set { savepath =value; }
    }

    /// <summary>
    /// 上传文件类型
    /// </summary>
    public string FileType {
        set { filetype = value; }
    }


    /// <summary>
    /// 上传文件大小,默认为200K
    /// </summary>
    public int FileSize {
        set { filesize = value; }
    }

    /// <summary>
    /// 命名方式:默认为自动命名(使用新名称),为false则使用原文件名.
    /// </summary>
    public bool NewName {
        set { newname = value; }
    }

    /// <summary>
    /// 是否生成缩略图
    /// </summary>
    public bool SmallImg {
        set { smallimg = value; }
    }

    /// <summary>
    /// 得到上传后文件名称
    /// </summary>
    public string FileName {
        get { return filename; }
    }

    public int SmallImgWidth {
        set { smallimgwidth = value; }
    }

    public int SmallImgHeight
    {
        set { smallimgheight = value; }
    }

    /// <summary>
    /// 生成图片水印类型
    /// </summary>
    public SY_Type SYType
    {
        set { sytype = value; }
    }


    /// <summary>
    /// 生成水印的图片地址或文字内容
    /// </summary>
    public string SYBody {
        set { sybody = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (HP1.NavigateUrl != "") {
            HP1.Target = "_blank";
            FU1.Visible = false;
            HP1.Visible = true;

            Button1.Visible = false;
            Button2.Visible = false;
            Button3.Visible = false;
            Button4.Visible = true;
            InputValue.Visible = false;
        }

    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        UpFile();

        HP1.NavigateUrl = filename;
        HP1.Target = "_blank";
        FU1.Visible = false;
        HP1.Visible = true;
        Button1.Visible = false;
        Button3.Visible = false;
        Button4.Visible = true;
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        File.Delete(Server.MapPath(HP1.NavigateUrl));
        HP1.NavigateUrl = "";
        FU1.Visible = true;
        HP1.Visible = false;
        Button1.Visible = true;
        Button2.Visible = false;
        Button3.Visible = true;
        Button4.Visible = false;
        InputValue.Visible = false;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        FU1.Visible = false;
        HP1.Visible = false;
        InputValue.Visible = true;
        InputValue.Text = savepath;
        Button1.Visible = false;
        Button2.Visible = true;
        Button3.Visible = false;
        Button4.Visible = false;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        filename = InputValue.Text;

        HP1.NavigateUrl = filename;
        HP1.Target = "_blank";
        FU1.Visible = false;
        HP1.Visible = true;
        InputValue.Visible = false;
        Button1.Visible = false;
        Button2.Visible = false;
        Button3.Visible = false;
        Button4.Visible = true;

    }

    protected void UpFile() {
        if (FU1.HasFile)
        {
            //检测文件大小
            if (FU1.PostedFile.ContentLength / 1024 > filesize)
            {
                AlertStop("上传文件太大!超过" + filesize + "K");
            }

            //检测文件类型
            string Type = FU1.PostedFile.FileName;
            Type = Type.Substring(Type.LastIndexOf(".")+1).ToUpper();
            filetype = filetype.ToUpper();
            if (filetype.IndexOf(Type) == -1)
            {
                AlertStop("上传文件类型有误!支持格式有:" + filetype+"!");
            }

            //确定上传文件的新名称
            string fname = "";
            if (newname)
            {
                string nowdate = DateTime.Now.ToString("yyyyMdhms");
                Random r = new Random();
                int a = r.Next(1000);
                fname = nowdate + a.ToString()+"." + Type;
            }
            else
            {
                fname = FU1.FileName;
                if (System.IO.File.Exists(Server.MapPath(savepath) + fname))
                {
                    AlertStop("文件" + fname + "已经存在!");
                }
            }

            filename = savepath + fname;

            switch (sytype) {
                case SY_Type.NoSY:
                    FU1.SaveAs(Server.MapPath(filename));
                    break;
                case SY_Type.ImgSY:
                    AddWaterPic();
                    break;
                case SY_Type.WordSY:
                    AddWater();
                    break;
            }
            if (smallimg) {
                AddSamllImg(fname);//生成缩略图
            }

        }
        else {
            Response.Write("<script>alert('请选择要上传的文件!');history.back();</script>");
            Response.End();
        }
    }


    protected void AlertStop(string str) {
        Response.Write("<script>alert('" + str + "');history.back();</script>");
        Response.End();
    }


    protected void AddSamllImg(string fname) {

        //生成的缩略图名称  
        string SmallImgName =Server.MapPath(savepath) + "S_" + fname;

        //从文件上传控件中取得图片对象  
        //System.Drawing.Image image = System.Drawing.Image.FromStream(fu.PostedFile.InputStream, true);
        //图片地址取得图片对象
        System.Drawing.Image image = System.Drawing.Image.FromFile(filename);

        System.Double NewWidth, NewHeight;
        if (image.Width > image.Height)
        {
            NewWidth = smallimgwidth;
            NewHeight = image.Height * (NewWidth / image.Width);
        }
        else
        {
            NewHeight = smallimgheight;
            NewWidth = (NewHeight / image.Height) * image.Width;
        }

        if (NewWidth > smallimgwidth)
        {
            NewWidth = smallimgwidth;
        }
        if (NewHeight > smallimgheight)
        {
            NewHeight = smallimgheight;
        }

        //取得图片大小  
        System.Drawing.Size I_size = new Size((int)NewWidth, (int)NewHeight);
        //新建一个bmp图片  
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(I_size.Width, I_size.Height);
        //新建一个画板  
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        //设置高质量插值法  
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度  
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //清空一下画布  
        g.Clear(Color.White);
        //在指定位置画图  
        g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
        new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
        System.Drawing.GraphicsUnit.Pixel);
        //保存高清晰度的缩略图  
        bitmap.Save(SmallImgName, System.Drawing.Imaging.ImageFormat.Jpeg);

        g.Dispose();
        image.Dispose();
        bitmap.Dispose();
        //生成缩略结束
    }


    /**/
    /// <summary>
    /// 在图片上增加文字水印
    /// </summary>
    protected void AddWater()
    {
        string addText = sybody;
        System.Drawing.Image image = System.Drawing.Image.FromStream(FU1.PostedFile.InputStream, true);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.DrawImage(image, 0, 0, image.Width, image.Height);
        System.Drawing.Font f = new System.Drawing.Font("Verdana", 20);
        System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Black);

        g.DrawString(addText, f, b,image.Width-350,image.Height-40);
        g.Dispose();

        //Response.Write(filename);
        //Response.End();
        image.Save(Server.MapPath(filename));
        image.Dispose();
    }

    /**/
    /// <summary>
    /// 在图片上生成图片水印
    /// </summary>
    /// <param name="Path">原服务器图片路径</param>
    /// <param name="Path_syp">生成的带图片水印的图片路径</param>
    /// <param name="Path_sypf">水印图片路径</param>
    protected void AddWaterPic()
    {
        System.Drawing.Image image = System.Drawing.Image.FromStream(FU1.PostedFile.InputStream, true);
        System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Server.MapPath(sybody));
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
        g.Dispose();

        image.Save(Server.MapPath(filename));
        image.Dispose();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值