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

    这是在Control里使用的代码,是在后台管理需要上传图片时使用的,不过我在这犯了一个错误, Request.Files[inputName];inputName名字中的大小写
    <input type="file" name="image">应该与model里的字段的名字一致
    if (Request.RequestType == "POST")
                {
                    foreach (string inputName in Request.Files.AllKeys)
                    {
                        HttpPostedFileBase file = Request.Files[inputName];
                        //存入文件
                        if (file.ContentLength > 0)
                        {
                            PropertyInfo pro = model.GetType().GetProperty(inputName);
                            string proValue = (string)pro.GetValue(model, null);
                            if (!string.IsNullOrWhiteSpace(proValue))
                            {
                                Common.FileUpload.DeleteFile(proValue);

                            }
                            string fileName = Common.FileUpload.UploadFile(file);
                            pro.SetValue(model, fileName, null);
                        }
                    }
    }

        public static string UploadFile(HttpPostedFileBase file)
            {

                // Check if we have a file

                if (null == file) return "";

                // Make sure the file has content

                if (!(file.ContentLength > 0)) return "";

                string fileName = file.FileName;

                string fileExt = Path.GetExtension(file.FileName);

                // Make sure we were able to determine a proper

                // extension

                if (null == fileExt) return "";
                char DirSeparator = '/';
                string FilesPath = string.Format("{0}team{0}{1}{0}{2}{3}{0}", DirSeparator, DateTime.Now.Year, DateTime.Now.Month.ToString().PadLeft(2, '0'), DateTime.Now.Day.ToString().PadLeft(2, '0'));
                string PhysicalPath = HttpContext.Current.Server.MapPath(FilesPath);
                // Check if the directory we are saving to exists

                if (!Directory.Exists(PhysicalPath))
                {

                    // If it doesn't exist, create the directory

                    Directory.CreateDirectory(PhysicalPath);

                }

                // Set our full path for saving

                string path = PhysicalPath + fileName;

                // Save our file

                file.SaveAs(Path.GetFullPath(path));

                // Return the filename


                return FilesPath + fileName;

            }

            public static void DeleteFile(string fileName)
            {
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    // Don't do anything if there is no name

                    if (fileName.Length == 0) return;

                    // Set our full path for deleting

                    string path = HttpContext.Current.Server.MapPath(fileName);

                    // Check if our file exists

                    if (File.Exists(Path.GetFullPath(path)))
                    {

                        // Delete our file

                        File.Delete(Path.GetFullPath(path));

                    }
                }
            }

  • 相关阅读:
    java程序员面试金典--i++
    JavaScript 装逼指南
    轻松学习 JavaScript——第 8 部分:JavaScript 中的类
    轻松学习 JavaScript——第 7 部分:对象属性描述符
    轻松学习 JavaScript——第 6 部分:JavaScript 箭头函数
    轻松学习 JavaScript——第 5 部分:简化函数提升
    轻松学习 JavaScript——第 4 部分:函数中的 arguments 对象
    轻松学习 JavaScript——第 3 部分:函数中的默认参数
    布隆过滤器
    决策树(ID3、C4.5、CART)
  • 原文地址:https://www.cnblogs.com/xiaoweizi/p/3605386.html
Copyright © 2011-2022 走看看