예제 #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>
        /// 使用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);
        }
예제 #3
0
        /// <summary>
        /// 将临时文件移到正式使用的位置
        /// </summary>
        /// <param name="sourceFileName"></param>
        /// <param name="destFileName"></param>
        public string TempFileMoveToUseFile(string tempPath, string replaceTempName)
        {
            if (string.IsNullOrEmpty(tempPath))
            {
                return("");
            }
            tempPath = HttpContext.Current.Server.MapPath(tempPath);
            string fileName      = Path.GetFileName(tempPath);
            string directoryName = Path.Combine(ConfigHelper.GetFullPath("UploadFilesSavePath"), replaceTempName);

            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            string fileUrl = Path.Combine(directoryName, fileName);

            File.Move(tempPath, fileUrl);

            return(fileUrl.Replace(HttpContext.Current.Server.MapPath("~"), "~/").Trim().Replace(@"\", @"/"));
        }