/// <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 } }); }
/// <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 = DateTimeOffset.Now, OriginalFileName = fileOption.FileName, Target = UploadTarget.Local }; return(await Task.FromResult(result).ConfigureAwait(false)); }
/// <summary> /// remote upload file /// </summary> /// <param name="remoteOption">remote options</param> /// <param name="fileOption">file option</param> /// <param name="file">upload file</param> /// <param name="parameters">parameters</param> /// <returns></returns> public static async Task <UploadResult> RemoteUploadAsync(RemoteOption remoteOption, UploadFile fileOption, byte[] file, object parameters = null) { if (fileOption == null) { throw new ArgumentNullException(nameof(fileOption)); } Dictionary <string, string> parameterDic = null; if (parameters != null) { parameterDic = parameters.ObjectToStringDcitionary(); } return(await RemoteUploadAsync(remoteOption, new List <UploadFile>() { fileOption }, new Dictionary <string, byte[]>() { { "file1", file } }, parameterDic).ConfigureAwait(false)); }