主题: .net上传图片
作者: 回首阑珊, 发布日期: 2009-10-27 17:55:16, 浏览数: 3965
using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class kindeditor_aspx_upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UpImage();

    }

    private void UpImage()
    {
        try
        {
            //文件保存跟目录
            string saveTopPath = Server.MapPath("~/editorUpload");

            //当前年
            string year = DateTime.Now.Year.ToString();
            //当前月
            string month = DateTime.Now.Month.ToString();
            //当前日
            string day = DateTime.Now.Day.ToString();

            //文件保存目录
            string savePath = saveTopPath + "\\" + year + "\\" + month + "\\" + day;

            //判断文件夹是否存在不存在则创建文件夹
            if (!Directory.Exists(saveTopPath + "\\" + year))
            {
                Directory.CreateDirectory(saveTopPath + "\\" + year);
            }
            if (!Directory.Exists(saveTopPath + "\\" + year + "\\" + month))
            {
                Directory.CreateDirectory(saveTopPath + "\\" + year + "\\" + month);
            }
            if (!Directory.Exists(saveTopPath + "\\" + year + "\\" + month + "\\" + day))
            {
                Directory.CreateDirectory(saveTopPath + "\\" + year + "\\" + month + "\\" + day);
            }

            //文件保存目录URL
            string saveUrl = "/editorUpload/" + year + "/" + month + "/" + day + "/";

            //最大文件大小
            int maxSize = 0;

            HttpPostedFile file = Request.Files.Get("imgFile");
            maxSize = file.ContentLength / 1024 / 1024;
            if (maxSize > 3)
            {
                this.alert("最大支持3M文件上传");
                return;
            }

            //提前原文件路径和名字 如 f:\123\images\like.jpg
            string allFileName = file.FileName;
            //提取原文件名 "\\"为转移字符等于\ 提取以后文件名等于 like.jpg
            string testName = allFileName.Substring(allFileName.LastIndexOf("\\") + 1);
            //提取文件后缀
            string postfix = allFileName.Substring(allFileName.LastIndexOf(".") + 1).ToLower();

            if (postfix != "jpg" && postfix != "gif" && postfix != "bmp" && postfix != "png")
            {
                this.alert("只支持jpg-gif-bmp-png这几种格式!");
                return;
            }

            try
            {
                string fileGuid = Guid.NewGuid().ToString().Replace("-", "");
                //保存图片
                file.SaveAs(savePath + "\\" + fileGuid + "." + postfix);
                //插入图片,关闭层
                Response.Write("<html>");
                Response.Write("<head>");
                Response.Write("<title>Insert Image</title>");
                Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
                Response.Write("</head>");
                Response.Write("<body>");
                Response.Write("<script type=\"text/javascript\">parent.KE.plugin[\"image\"].insert(\"" + Request["id"] + "\", \"." + saveUrl + fileGuid + "." + postfix + "\",\"" + Request["imgTitle"] + "\",\"" + Request["imgWidth"] + "\",\"" + Request["imgHeight"] + "\",\"" + Request["imgBorder"] + "\");</script>");
                Response.Write("</body>");
                Response.Write("</html>");
            }
            catch
            {
                this.alert("上传失败!");
            }

        }
        catch
        {
            this.alert("上传失败!");
        }
    }

    private void alert(string msg)
    {
        Response.Write("<html>");
        Response.Write("<head>");
        Response.Write("<title>error</title>");
        Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
        Response.Write("</head>");
        Response.Write("<body>");
        Response.Write("<script type=\"text/javascript\">alert(\""+msg+"\");history.back();</script>");
        Response.Write("</body>");
        Response.Write("</html>");
    }
}

发表新帖 发表回复