• 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;
        }
  • 相关阅读:
    开发人员创建智能客户端的十大理由
    OpenStack 学习资料总结
    VirtualBox启用嵌套VTx/AMDV
    element ui table 表尾合计行 错位优化
    群友酒方,夜夜十次郎
    跨域 Better
    Unity 重命名一个字段,同时不丢失其序列化的值
    C++ static 变量
    编译安装apache2.4
    centos设置crontab定时执行shell脚本
  • 原文地址:https://www.cnblogs.com/honghong75042/p/5773942.html
Copyright © 2020-2023  润新知