Exemplo n.º 1
0
 private static void SaveFile(ICompatiblePostedFile file, string filePath)
 {
     using (var fs = new FileStream(filePath, FileMode.Create))
     {
         file.CopyToAsync(fs);
         fs.Flush();
     }
 }
Exemplo n.º 2
0
        private async void SaveFile(ICompatiblePostedFile imgFile, string targetPath)
        {
            using (FileStream fs = new FileStream(targetPath, FileMode.Create))
            {
                await imgFile.CopyToAsync(fs);

                fs.Flush();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string Upload(string dir, ICompatiblePostedFile file)
        {
            var dirPath  = Cms.PhysicPath + dir;
            var fileName = file.GetFileName();

            var filePath = dirPath + file.GetFileName();

            if (File.Exists(filePath))
            {
                return("{\"error\":\"文件已经存在\"}");
            }
            using (var fs = new FileStream(filePath, FileMode.Create))
            {
                file.CopyToAsync(fs);
                fs.Flush();
            }

            return("{\"url\":\"" + dir + fileName + "\"}");
        }
Exemplo n.º 4
0
        /// <summary>
        /// 处理上传
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task UploadRequest(ICompatibleHttpContext context)
        {
            context.Response.ContentType("application/json; charset=utf-8");
            var path = context.Request.GetPath();
            //String aspxUrl = path.Substring(0, path.LastIndexOf("/") + 1);
            String saveUrl = $"{(this._appPath == "/" ? "" : this._appPath)}/{this._rootPath}";

            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();

            extTable.Add("image", "gif,jpg,jpeg,png,bmp,webp");
            extTable.Add("flash", "swf,flv");
            extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
            extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,7z");

            //最大文件大小
            int maxSize = 10240000;

            ICompatiblePostedFile imgFile = context.Request.File("imgFile");

            if (imgFile == null)
            {
                return(this.showError(context, "请选择文件。"));
            }

            String dirPath = EnvUtil.GetBaseDirectory() + this._rootPath;

checkDir:
            bool isCreate = false;

            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath).Create();
                if (!isCreate)
                {
                    isCreate = true;
                    goto checkDir;
                }
                return(showError(context, "上传目录不存在。"));
            }

            String dirName = context.Request.Query("dir");

            if (String.IsNullOrEmpty(dirName))
            {
                dirName = "image";
            }

            if (!extTable.ContainsKey(dirName))
            {
                return(this.showError(context, "目录名不正确。"));
            }

            String fileName = imgFile.GetFileName();
            String fileExt  = Path.GetExtension(fileName).ToLower();

            if (imgFile == null || imgFile.GetLength() > maxSize)
            {
                return(this.showError(context, "上传文件大小超过限制。"));
            }

            if (String.IsNullOrEmpty(fileExt) ||
                Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                return(this.showError(context, "上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。"));
            }

            //创建文件夹
            dirPath += dirName + "/";
            saveUrl += dirName + "/";
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath).Create();
            }

            String ymd = DateTime.Now.ToString("yyyyMM", DateTimeFormatInfo.InvariantInfo);

            dirPath += ymd + "/";
            saveUrl += ymd + "/";
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            String originName  = UploadUtils.GetUploadFileName(imgFile);
            String newFileName = originName + fileExt;
            //String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;

            // 自动将重复的名称命名
            String targetPath = dirPath + newFileName;
            int    i          = 0;

            while (File.Exists(targetPath))
            {
                i++;
                newFileName = $"{originName}_{i.ToString()}{fileExt}";
                targetPath  = dirPath + newFileName;
            }

            using (FileStream fs = new FileStream(targetPath, FileMode.Create))
            {
                imgFile.CopyToAsync(fs);
                fs.Flush();
            }

            String fileUrl = saveUrl + newFileName;

            Hashtable hash = new Hashtable();

            hash["error"] = 0;
            hash["url"]   = fileUrl;
            return(context.Response.WriteAsync(JsonAnalyzer.ToJson(hash)));
        }