예제 #1
0
        /// <summary>
        /// 返回临时文件存放位置
        /// </summary>
        /// <param name="fileExtension"></param>
        /// <returns></returns>
        public string GetFileTempUrl(string fileExtension)
        {
            string sDay = DateTime.Now.Day.ToString();
            string dir  = ConfigHelper.GetFullPath("UploadFilesSavePath");

            dir = Path.Combine(dir, "temp");
            string cDir = Path.Combine(dir, sDay);

            if (!Directory.Exists(cDir))
            {
                Directory.CreateDirectory(cDir);
            }
            string[] childDirs = Directory.GetDirectories(dir);
            foreach (string item in childDirs)
            {
                if (item != (cDir))
                {
                    Directory.Delete(item, true);
                }
            }
            string fileName = CustomsHelper.GetFormatDateTime() + fileExtension.ToLower();
            string fileUrl  = Path.Combine(dir, sDay, fileName);

            return(fileUrl.Replace(HttpContext.Current.Server.MapPath("~"), "~/").Trim().Replace(@"\", @"/"));
        }
예제 #2
0
        /// <summary>
        /// 上传文件,并返回存储文件的虚拟路径,该虚拟路径包含根操作符(代字号 [~])
        /// </summary>
        /// <param name="file"></param>
        /// <param name="dirName"></param>
        /// <returns></returns>
        public string Upload(HttpPostedFile file, string dirName)
        {
            if (file == null || file.ContentLength == 0)
            {
                throw new ArgumentException("没有获取到任何上传的文件", "file");
            }
            dirName = "" + dirName.Trim('/') + "/";
            int    size          = file.ContentLength;
            string fileExtension = Path.GetExtension(file.FileName).ToLower();

            if (!IsFileValidated(file.InputStream, size))
            {
                throw new ArgumentException("上传文件不在规定的上传文件范围内");
            }
            uploadRoot = VirtualPathUtility.AppendTrailingSlash(uploadRoot);
            string fileUrl         = string.Empty;
            string fName           = CustomsHelper.GetFormatDateTime();
            string saveVirtualPath = uploadRoot + dirName + fName.Substring(0, 8) + "/";
            string fullPath        = HttpContext.Current.Server.MapPath(saveVirtualPath);

            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(fullPath);
            }
            fullPath += fName + fileExtension;
            file.SaveAs(fullPath);

            fileUrl = saveVirtualPath + fName + fileExtension;
            return(fileUrl);
        }
예제 #3
0
        /// <summary>
        /// 创建商品缩略图
        /// 返回值:主图(220*220)、大图(800*800)、中图(350*350)、小图(50*50)
        /// </summary>
        /// <param name="virtualPath">商品主图片的路径,该路径是带有“~”符号的路径</param>
        /// <returns></returns>
        public string[] GetProductThumbnailImages(string virtualPath)
        {
            if (string.IsNullOrEmpty(virtualPath))
            {
                return(null);
            }
            context = HttpContext.Current;
            string siteRootPath = context.Server.MapPath("~");
            string fullPath     = context.Server.MapPath(virtualPath);
            string dir          = Path.GetDirectoryName(fullPath);
            string fName        = Path.GetFileNameWithoutExtension(fullPath);
            string fExtension   = Path.GetExtension(fullPath).ToLower();
            string newDir       = dir + "\\" + fName;

            if (!Directory.Exists(newDir))
            {
                Directory.CreateDirectory(newDir);
            }
            ImagesHelper ih = new ImagesHelper();
            //创建220*220 主图
            string sImages = newDir + "\\" + CustomsHelper.GetFormatDateTime() + fExtension;

            ih.CreateThumbnailImage(fullPath, sImages, 220, 220);
            sImages = sImages.Replace(siteRootPath, "/").Trim().Replace(@"\", @"/");
            //创建800*800 大图
            string sLImages = newDir + "\\" + CustomsHelper.GetFormatDateTime() + fExtension;

            ih.CreateThumbnailImage(fullPath, sLImages, 800, 800);
            sLImages = sLImages.Replace(siteRootPath, "/").Trim().Replace(@"\", @"/");
            //创建350*350 中图
            string sMimages = newDir + "\\" + CustomsHelper.GetFormatDateTime() + fExtension;

            ih.CreateThumbnailImage(fullPath, sMimages, 350, 350);
            sMimages = sMimages.Replace(siteRootPath, "/").Trim().Replace(@"\", @"/");
            //创建50*50 小图
            string sSimages = newDir + "\\" + CustomsHelper.GetFormatDateTime() + fExtension;

            ih.CreateThumbnailImage(fullPath, sSimages, 50, 50);
            sSimages = sSimages.Replace(siteRootPath, "/").Trim().Replace(@"\", @"/");

            string[] images = { sImages, sLImages, sMimages, sSimages };
            return(images);
        }
예제 #4
0
        /// <summary>
        /// 上传文件到临时存储,并返回存储文件的虚拟路径,该虚拟路径包含根操作符(代字号 [~])
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public string UploadToTemp(HttpPostedFile file)
        {
            if (file == null || file.ContentLength == 0)
            {
                throw new ArgumentException("没有获取到任何上传的文件", "file");
            }
            int    size          = file.ContentLength;
            string fileExtension = Path.GetExtension(file.FileName).ToLower();

            if (!IsFileValidated(file.InputStream, size))
            {
                throw new ArgumentException("上传文件不在规定的上传文件范围内");
            }

            uploadRoot = VirtualPathUtility.AppendTrailingSlash(uploadRoot);
            string dirName = "Temp/";

            string[] childDirs = Directory.GetDirectories(HttpContext.Current.Server.MapPath(uploadRoot + dirName));
            foreach (string item in childDirs)
            {
                DirectoryInfo di = new DirectoryInfo(item);
                TimeSpan      ts = DateTime.Now - di.CreationTime;
                if (ts.Days > 2)
                {
                    Directory.Delete(item, true);
                }
            }

            string fName           = CustomsHelper.GetFormatDateTime();
            string fileUrl         = string.Empty;
            string saveVirtualPath = uploadRoot + dirName + fName.Substring(0, 8) + "";
            string fullPath        = HttpContext.Current.Server.MapPath(saveVirtualPath);

            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(fullPath);
            }
            fullPath += "/" + fName + fileExtension;
            file.SaveAs(fullPath);

            fileUrl = saveVirtualPath + "/" + fName + fileExtension;
            return(fileUrl);
        }
예제 #5
0
        /// <summary>
        /// 指定存储目录key,是否生成缩略图,上传文件
        /// 返回所有文件路径,如果是生成缩略图,则包含缩略图文件路径
        /// </summary>
        /// <param name="file"></param>
        /// <param name="key"></param>
        /// <param name="isCreateThumbnail"></param>
        /// <returns></returns>
        public string[] Upload(HttpPostedFile file, string key, bool isCreateThumbnail)
        {
            if (file == null || file.ContentLength == 0)
            {
                throw new ArgumentException("没有获取到任何上传的文件", "file");
            }
            int    size          = file.ContentLength;
            string fileExtension = Path.GetExtension(file.FileName).ToLower();

            if (!IsFileValidated(file.InputStream, size))
            {
                throw new ArgumentException("上传文件不在规定的上传文件范围内");
            }
            if (isCreateThumbnail)
            {
                if ((fileExtension != ".jpg") || (fileExtension != ".jpg"))
                {
                    throw new ArgumentException("创建缩略图只支持.jpg格式的文件,请检查");
                }
            }
            string dir = ConfigHelper.GetValueByKey(key);

            if (string.IsNullOrWhiteSpace(dir))
            {
                throw new ArgumentException("未找到" + key + "的相关配置,请检查", "key");
            }

            string paths = "";

            dir = VirtualPathUtility.AppendTrailingSlash(dir);
            string rndName  = CustomsHelper.GetFormatDateTime();
            string fName    = rndName + fileExtension;
            string filePath = dir + rndName.Substring(0, 8) + "/";
            string fullPath = HttpContext.Current.Server.MapPath(filePath);

            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(fullPath);
            }
            file.SaveAs(fullPath + fName);

            paths += filePath + fName;
            if (isCreateThumbnail)
            {
                ImagesHelper ih           = new ImagesHelper();
                string[]     whBPicture   = ConfigHelper.GetValueByKey("BPicture").Split(',');
                string[]     whMPicture   = ConfigHelper.GetValueByKey("MPicture").Split(',');
                string[]     whSPicture   = ConfigHelper.GetValueByKey("SPicture").Split(',');
                string       bPicturePath = filePath + rndName + "_b" + fileExtension;
                string       mPicturePath = filePath + rndName + "_m" + fileExtension;
                string       sPicturePath = filePath + rndName + "_s" + fileExtension;
                ih.CreateThumbnailImage(fullPath + fName, HttpContext.Current.Server.MapPath(bPicturePath), int.Parse(whBPicture[0]), int.Parse(whBPicture[1]));
                ih.CreateThumbnailImage(fullPath + fName, HttpContext.Current.Server.MapPath(mPicturePath), int.Parse(whMPicture[0]), int.Parse(whMPicture[1]));
                ih.CreateThumbnailImage(fullPath + fName, HttpContext.Current.Server.MapPath(sPicturePath), int.Parse(whSPicture[0]), int.Parse(whSPicture[1]));
                paths += "," + bPicturePath;
                paths += "," + mPicturePath;
                paths += "," + sPicturePath;
            }
            else
            {
                paths += "," + filePath + fName;
                paths += "," + filePath + fName;
                paths += "," + filePath + fName;
            }

            return(paths.Split(','));
        }