/// <summary> /// Local upload file /// </summary> /// <param name="uploadOption">Upload option</param> /// <param name="fileOption">File option</param> /// <param name="file">File</param> /// <returns>upload file result</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.Empty; if (uploadOption.SaveToContentRoot) { var contentFolder = string.IsNullOrWhiteSpace(uploadOption.ContentRootPath) ? DefaultContentFolder : uploadOption.ContentRootPath; savePath = Path.IsPathRooted(savePath) ? Path.Combine(savePath, contentFolder) : Path.Combine(contentFolder, savePath); } if (!string.IsNullOrWhiteSpace(fileOption.Folder)) { savePath = Path.Combine(savePath, fileOption.Folder); } string realSavePath = savePath; if (!Path.IsPathRooted(realSavePath)) { realSavePath = Path.Combine(Directory.GetCurrentDirectory(), realSavePath); } 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 ?? string.Empty, 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> /// Local upload file /// </summary> /// <param name="uploadOption">Upload option</param> /// <param name="fileOption">File option</param> /// <param name="file">File</param> /// <returns>Return the upload result</returns> public static UploadResult LocalUpload(UploadOption uploadOption, UploadFile fileOption, byte[] file) { return(LocalUploadAsync(uploadOption, fileOption, file).Result); }
/// <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>Return the upload result</returns> public static UploadResult RemoteUpload(RemoteServerOption remoteOption, UploadFile fileOption, byte[] file, object parameters = null) { return(RemoteUploadAsync(remoteOption, fileOption, file, parameters).Result); }
/// <summary> /// Local upload file /// </summary> /// <param name="uploadOption">Upload option</param> /// <param name="fileOption">File option</param> /// <param name="file">File</param> /// <returns>Return the upload result</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> /// Remote upload file /// </summary> /// <param name="remoteOption">Remote option</param> /// <param name="fileOption">File option</param> /// <param name="file">Upload file</param> /// <param name="parameters">Parameters</param> /// <returns>Return the upload result</returns> public static async Task <UploadResult> RemoteUploadAsync(RemoteServerOption 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)); }