VS2008下web页面上传图片的自定义控件源码

5 篇文章 0 订阅

本代码来自网络,非本人原创。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace UploadImage
{
    [DefaultProperty("Text"),
   ToolboxData(@"<{0}:UpImage runat=server></{0}:UpImage>")]
    public class UpImage : Control, INamingContainer
    {
        protected int filelength;
        protected string imageUrl;
        protected string mydirectory;
        static string LogoURL;
        protected string vpicture;
        public Button mybutton;
        public HtmlInputFile fileUpload;
        public Label Label1;

        public UpImage()
        {
            this.EnsureChildControls();
        }
        [Bindable(true), Category("Appearance"), DefaultValue("")]
        [DescriptionAttribute("文件大小")]
        public int FileLength
        {
            set { filelength = value; }
            get { return filelength; }
        }
        [DescriptionAttribute("图片名字")]
        public string ImageUrl
        {
            set { imageUrl = value; }
            get { return imageUrl; }
        }
        [DescriptionAttribute("文件路径")]
        public string MyDirectory
        {
            get { return mydirectory; }
            set { mydirectory = value; }
        }
        [DescriptionAttribute("图片的相对地址")]
        public string Logo
        {
            get { return LogoURL; }
            set { LogoURL = value; }
        }
        [DescriptionAttribute("是否显示图片")]
        public string vPicture
        {
            set { vpicture = value; }
            get { return vpicture; }
        }
        private void mybutton_Click(object sender, System.EventArgs e)
        {
            if (!fileUpload.Value.ToString().Equals(""))
            {
                LogoURL = fileUpload.PostedFile.FileName.ToString();
                LogoURL = LogoURL.Substring(LogoURL.LastIndexOf("."), (LogoURL.Length - LogoURL.LastIndexOf(".")));
                if (fileUpload.PostedFile.ContentLength > filelength)
                {
                    myScript("图片超过指定大小!");
                }
                else
                {
                    if (LogoURL.Equals(".jpg") || LogoURL.Equals(".bmp") || LogoURL.Equals(".gif"))
                    {
                        LogoURL = mydirectory + "//" + imageUrl + LogoURL;
                        mydirectory = Page.Server.MapPath(" ") + "//" + mydirectory;
                        if (Directory.Exists(mydirectory))
                        { }
                        else
                        {
                            Directory.CreateDirectory(mydirectory);
                        }
                        fileUpload.PostedFile.SaveAs(Page.Server.MapPath(" ") + "//" + LogoURL);
                        if (vpicture.Equals("1"))
                        {
                            Label1.Text = "<img width='100' heigth='100' src='" + LogoURL + "'>";
                        }
                        myScript("图片上传成功!");
                    }
                    else
                    {
                        myScript("文件类型不对!");
                    }
                }
            }
        }
        protected void myScript(string msg)
        {           
            Page.ClientScript.RegisterStartupScript(this.GetType(),"","<script language='JavaScript'>alert('" + msg + "');</script>"); //此处我进行了修正
        }
        protected override void CreateChildControls()
        {
            mybutton = new Button();
            fileUpload = new HtmlInputFile();
            Label1 = new Label();
            mybutton.Text = "提交";
            this.Controls.Add(fileUpload);
            this.Controls.Add(mybutton);
            this.Controls.Add(new LiteralControl("<p>"));
            this.Controls.Add(Label1);
            this.Controls.Add(new LiteralControl("</p>"));
            mybutton.Click += new EventHandler(mybutton_Click);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值