Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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(@"\", @"/"));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 将临时存储文件转移到商品文件,并返回存储文件的虚拟路径,该虚拟路径包含根操作符(代字号 [~])
        /// </summary>
        /// <param name="tempVirtualPath"></param>
        /// <returns></returns>
        public string FromTempToProduct(string tempVirtualPath)
        {
            uploadRoot = VirtualPathUtility.AppendTrailingSlash(uploadRoot);
            string pVirtualPath = uploadRoot + "Product/" + CustomsHelper.CreateDateTimeString().Substring(0, 6) + "/";

            return(TempFileToUseFile(tempVirtualPath, pVirtualPath));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 使用FileUpload服务器控件上传文件
        /// </summary>
        /// <param name="fu"></param>
        /// <param name="path"></param>
        /// <param name="dic"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public bool Upload(FileUpload fu, string path, ref Dictionary <string, string> dic, ref string errorMsg)
        {
            if (!fu.HasFile)
            {
                return(false);
            }

            //判断文件的类型和大小
            string type = fu.PostedFile.ContentType;
            int    size = fu.PostedFile.ContentLength;
            //获取客户端上的文件的完全限定名称
            string fileOriginalName = fu.PostedFile.FileName;
            //获取文件扩展名
            string fileExtension = Path.GetExtension(fu.PostedFile.FileName).ToLower();
            //获取不包含文件扩展名的文件名
            string fileName = CustomsHelper.CreateDateTimeString();
            //文件保存路径
            string fileUrl = string.Empty;

            if (!IsFileValidated(fu.PostedFile.InputStream, size))
            {
                errorMsg = "上传的文件为禁止的文件!";
                return(false);
            }

            string dirPath = HttpContext.Current.Server.MapPath(path);
            //创建保存文件的路径
            string fullPath = path + fileName + fileExtension;

            try
            {
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }

                //上载文件到硬盘
                fu.SaveAs(HttpContext.Current.Server.MapPath(fullPath));

                fileUrl = fullPath;

                dic.Add("FileName", fileName);
                dic.Add("FileExtension", fileExtension);
                dic.Add("FileUrl", fileUrl);
                dic.Add("FileOriginalName", fileOriginalName);

                return(true);
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
            }

            return(false);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 使用FileUpload服务器控件上传文件
        /// </summary>
        /// <param name="fu"></param>
        /// <param name="dic"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public bool UploadFile(FileUpload fu, string configKey, ref Dictionary <string, string> dic, ref string errorMsg)
        {
            if (!fu.HasFile)
            {
                return(false);
            }

            //判断文件的类型和大小
            string type = fu.PostedFile.ContentType;
            int    size = fu.PostedFile.ContentLength;
            //获取客户端上的文件的完全限定名称
            string fileOriginalName = fu.PostedFile.FileName;
            //获取文件扩展名
            string fileExtension = Path.GetExtension(fu.PostedFile.FileName).ToLower();
            //获取不包含文件扩展名的文件名
            string fileName = CustomsHelper.CreateDateTimeString();
            //文件保存路径
            string fileUrl = string.Empty;
            int    fileLen = fu.PostedFile.ContentLength;

            if (!IsFileValidated(fu.PostedFile.InputStream, fileLen))
            {
                errorMsg = "上传的文件为禁止的文件!";
                return(false);
            }

            //创建保存文件的路径
            string path     = ConfigHelper.GetFullPath(configKey);
            string fullPath = path + fileName + fileExtension;

            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                //上载文件到硬盘
                fu.SaveAs(fullPath);

                fileUrl = ConfigHelper.GetValueByKey(configKey) + fileName + fileExtension;

                dic.Add("FileName", fileName);
                dic.Add("FileExtension", fileExtension);
                dic.Add("FileUrl", fileUrl);
                dic.Add("FileOriginalName", fileOriginalName);

                return(true);
            }
            catch
            {
            }

            return(false);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }