• 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;
        }
  • 相关阅读:
    ros 使用命令测试topic
    python unicode
    python ros 回充demo
    python ros 回充调用demo
    flask报错No module named 'flask.ext'
    python flask 接口
    ros 安装c++编译的可执行文件
    Linux-Ubuntu14.04下mongodb安装部署
    如何在Ubuntu 14.04中安装最新版Eclipse
    ubuntu 14.04 安装redis5.0.3
  • 原文地址:https://www.cnblogs.com/honghong75042/p/3254437.html
Copyright © 2020-2023  润新知