zoukankan      html  css  js  c++  java
  • c#上传图片

    private string uploadPic()
        {
            string ImageURL = "";
            //检测一下uploadFile是否选择了上传文件
            if (uploadFile.HasFile)
            {
                //检测一下目录是否存在
                if (!Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "upImages//adv//"))
                {
                    Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath + "upImages//adv//");
                }
                string savePath = @"upImagesadv";
                if (uploadFile.HasFile)
                {
                    savePath += uploadFile.FileName;
                }
                if (!File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "upImages//adv//" + uploadFile.FileName))
                {
                    string[] allowesExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                    string fileEctension = System.IO.Path.GetExtension(uploadFile.FileName).ToLower();
                    bool fileOK = false;
                    for (int i = 0; i < allowesExtensions.Length; i++)
                    {
    
                        if (fileEctension == allowesExtensions[i])
                        {
    
                            fileOK = true;
    
                        }
    
                    }
                    if (!fileOK)
                    {
                        lblPicMsg.Visible = true;
                        lblPicMsg.Text = "不支持该类型的文件";
                    }
                    else
                    {
                        if (uploadFile.FileContent.Length / 1024 > 4096)
                        {
                            lblPicMsg.Visible = true;
                            lblPicMsg.Text = "上传文件大小限制为 4MB !";
                        }
                        else
                        {
                            string AutoName = "GTT" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + System.IO.Path.GetExtension(uploadFile.FileName).ToString();
                            string path = Server.MapPath("~") + @"upImagesadv" + AutoName;
                            uploadFile.PostedFile.SaveAs(path);
                            lblPicMsg.Visible = true;
                            lblPicMsg.Text = "文件上传成功!" + "<br>";
                            lblPicMsg.Text = lblPicMsg.Text + "文件名称:" + uploadFile.FileName + "<br>";
                            lblPicMsg.Text = lblPicMsg.Text + "文件类型:" + System.IO.Path.GetExtension(uploadFile.FileName).ToString() + "<br>";
                            lblPicMsg.Text = lblPicMsg.Text + "文件大小:" + uploadFile.FileContent.Length / 1024 + " KB" + "<br>";
                            ImageURL = @"upImagesadv" + AutoName;
                            //将预览图层内的图片控件显示,并将生成的缩略图地址绑定到该图片上
                            showimg.Visible = true;
                            showimg.Src = HttpContext.Current.Request.PhysicalApplicationPath + "upImages//adv//" + AutoName;
                            //清除上传图片后http数据缓冲
                            Response.Clear();
                        }
                    }
                }
                else
                {
                    lblPicMsg.Text = uploadFile.FileName + "文件已经存在,重新上传附件!";
                }
            }
            else
            {
                lblPicMsg.Visible = true;
                lblPicMsg.Text = "请选择上传文件 !";
            }
            ImageURL = ImageURL.Replace("\", "/");
            return ImageURL;
        }
  • 相关阅读:
    bzoj1529: [POI2005]ska Piggy banks
    BZOJ 3065 带插入区间K小值
    【BZOJ做题记录】07.07~?
    【BZOJ2882】【字符串的最小表示】工艺
    【不能继续浪啦】BZ做题记录[7.01~7.06]
    第一次到zhzx到今天已经一周年了
    BestCoder Round #45
    BestCoder Round #41 记。
    BZOJ做题记录[0512~?]
    [BZOJ2809&1455&1367]解题报告|可并堆
  • 原文地址:https://www.cnblogs.com/honghong75042/p/3254437.html
Copyright © 2011-2022 走看看