コード例 #1
0
        /// <summary>
        /// local upload file
        /// </summary>
        /// <param name="uploadOption">upload option</param>
        /// <param name="fileOptions">file options</param>
        /// <param name="files">files</param>
        /// <returns></returns>
        public static async Task <UploadResult> LocalUploadAsync(UploadOption uploadOption, List <UploadFile> fileOptions, Dictionary <string, byte[]> files)
        {
            if (fileOptions.IsNullOrEmpty() || files == null || files.Count <= 0)
            {
                return(new UploadResult()
                {
                    Code = "400",
                    ErrorMessage = "没有指定任何上传文件配置或文件信息",
                    Success = false
                });
            }
            UploadResult result = new UploadResult()
            {
                Success = true,
                Files   = new List <UploadFileResult>(0)
            };

            foreach (var fileItem in files)
            {
                string fileName   = fileItem.Key;
                var    fileConfig = fileOptions.FirstOrDefault(c => c.FileName == fileName);
                if (fileConfig == null)
                {
                    fileConfig = new UploadFile()
                    {
                        FileName = fileName,
                        Rename   = false
                    };
                }
                var fileResult = await LocalUploadFileAsync(uploadOption, fileConfig, fileItem.Value);

                result.Files.Add(fileResult);
            }
            return(result);
        }
コード例 #2
0
 static UploadManager()
 {
     Default = new UploadOption()
     {
         Remote = false
     };
 }
コード例 #3
0
        /// <summary>
        ///  local upload file
        /// </summary>
        /// <param name="uploadOption">upload option</param>
        /// <param name="fileOption">file option</param>
        /// <param name="file">file</param>
        /// <returns></returns>
        public static async Task <UploadResult> LocalUploadAsync(UploadOption uploadOption, UploadFile fileOption, byte[] file)
        {
            var fileResult = await LocalUploadFileAsync(uploadOption, fileOption, file).ConfigureAwait(false);

            return(new UploadResult()
            {
                Code = "200",
                Success = true,
                Files = new List <UploadFileResult>()
                {
                    fileResult
                }
            });
        }
コード例 #4
0
        /// <summary>
        /// upload by upload option
        /// </summary>
        /// <param name="uploadOption">upload option</param>
        /// <param name="fileOptions">file options</param>
        /// <param name="files">files</param>
        /// <param name="parameters">parameters</param>
        /// <returns></returns>
        public static async Task <UploadResult> UploadByOptionAsync(UploadOption uploadOption, IEnumerable <UploadFile> fileOptions, Dictionary <string, byte[]> files, Dictionary <string, string> parameters = null)
        {
            if (files == null || files.Count <= 0)
            {
                throw new ArgumentNullException(nameof(files));
            }
            if (uploadOption == null)
            {
                throw new ArgumentNullException(nameof(uploadOption));
            }
            UploadResult uploadResult = null;

            if (uploadOption.Remote)
            {
                uploadResult = await RemoteUploadAsync(uploadOption.GetRemoteOption(), fileOptions.ToList(), files, parameters).ConfigureAwait(false);
            }
            else
            {
                uploadResult = await LocalUploadAsync(uploadOption, fileOptions.ToList(), files).ConfigureAwait(false);
            }
            return(uploadResult);
        }
コード例 #5
0
 /// <summary>
 /// config default upload
 /// </summary>
 /// <param name="uploadOption">upload option</param>
 public static void ConfigDefaultUpload(UploadOption uploadOption)
 {
     Default = uploadOption;
 }
コード例 #6
0
        /// <summary>
        /// local upload file
        /// </summary>
        /// <param name="uploadOption">upload option</param>
        /// <param name="fileOption">file option</param>
        /// <param name="file">file</param>
        /// <returns></returns>
        static async Task <UploadFileResult> LocalUploadFileAsync(UploadOption uploadOption, UploadFile fileOption, byte[] file)
        {
            #region verify parameters

            if (uploadOption == null)
            {
                throw new ArgumentNullException(nameof(uploadOption));
            }
            if (fileOption == null)
            {
                throw new ArgumentNullException(nameof(fileOption));
            }

            #endregion

            #region set save path

            string savePath     = uploadOption.SavePath;
            string realSavePath = savePath;

            if (realSavePath.IsNullOrEmpty())
            {
                realSavePath = Directory.GetCurrentDirectory();
            }
            realSavePath = Path.Combine(realSavePath, fileOption.Folder ?? string.Empty);
            if (!Path.IsPathRooted(realSavePath))
            {
                if (uploadOption.SaveToContentRoot)
                {
                    savePath = Path.Combine(string.IsNullOrWhiteSpace(uploadOption.ContentRootPath) ? "content" : uploadOption.ContentRootPath, realSavePath);
                }
                realSavePath = Path.Combine(Directory.GetCurrentDirectory(), savePath);
            }
            if (!Directory.Exists(realSavePath))
            {
                Directory.CreateDirectory(realSavePath);
            }

            #endregion

            #region file suffix

            string suffix = Path.GetExtension(fileOption.FileName).Trim('.');
            if (!string.IsNullOrWhiteSpace(fileOption.Suffix))
            {
                suffix = fileOption.Suffix.Trim('.');
            }

            #endregion

            #region file name

            string fileName = Path.GetFileNameWithoutExtension(fileOption.FileName);
            if (fileOption.Rename)
            {
                fileName = Guid.NewGuid().ToInt64().ToString();
            }
            fileName = string.Format("{0}.{1}", fileName, suffix);

            #endregion

            #region save file

            string fileFullPath = Path.Combine(realSavePath, fileName);
            File.WriteAllBytes(fileFullPath, file);
            string relativePath = Path.Combine(savePath, fileName);

            #endregion

            var result = new UploadFileResult()
            {
                FileName         = fileName,
                FullPath         = fileFullPath,
                Suffix           = Path.GetExtension(fileName).Trim('.'),
                RelativePath     = relativePath,
                UploadDate       = DateTime.Now,
                OriginalFileName = fileOption.FileName,
                Target           = UploadTarget.Local
            };
            return(await Task.FromResult(result).ConfigureAwait(false));
        }