using Atomx.Utils.Json; using Atomx.Utils.Models; using Microsoft.AspNetCore.Http; using System.Net; namespace Atomx.Utils.Extension { public class UploadExtension { /// /// 上传 /// /// 文件 /// 站点根目录 /// 存储目录 /// 是否固定存储在根目录,如果不固定,将自动根据时间生成子文件夹进行存储 /// 文件名 /// 覆盖上传的路径 /// 文件类型 /// public Result LocalUpload(IFormFile file, string rootPath, string path, bool fixedPath, string fileName, string currentPath, string extension) { var result = new Result(); var filepath = string.Empty; if (!string.IsNullOrEmpty(currentPath)) { filepath = currentPath; } else { filepath = PathExtension.GetFileNameByData(path, fixedPath, fileName, extension); } var savepath = Path.Combine(rootPath, filepath.Replace("/", "\\")); var newpath = Path.GetDirectoryName(savepath); if (string.IsNullOrEmpty(newpath)) { result.Message = "Storage path exception"; result.Success = false; return result; } try { if (!Directory.Exists(newpath)) { Directory.CreateDirectory(newpath); } } catch { result.Message = "Folder failure is established"; result.Success = false; return result; } try { using (FileStream fs = File.Create(savepath)) { file.CopyTo(fs); fs.Flush(); } } catch (Exception ex) { result.Message = ex.Message; result.Success = false; } result.Data = filepath; return result; } /// /// 上传 /// /// 文件 /// 站点根目录 /// 存储目录 /// 是否固定存储在根目录,如果不固定,将自动根据时间生成子文件夹进行存储 /// 文件名 /// 覆盖上传的路径 /// 文件类型 /// public async Task> LocalUploadEncryptAsync(IFormFile file, string rootPath, string path, bool fixedPath, string fileName, string currentPath, string extension) { var result = new Result(); var filepath = string.Empty; if (!string.IsNullOrEmpty(currentPath)) { filepath = currentPath; } else { filepath = PathExtension.GetFileNameByData(path, fixedPath, fileName, extension); } var savepath = Path.Combine(rootPath, filepath.Replace("/", "\\")); var newpath = Path.GetDirectoryName(savepath); if(string.IsNullOrEmpty(newpath)) { result.Message = "Storage path exception"; result.Success = false; return result; } try { if (!Directory.Exists(newpath)) { Directory.CreateDirectory(newpath); } } catch { result.Message = "Folder failure is established"; result.Success = false; return result; } try { var stream = file.OpenReadStream(); byte[] bytes = new byte[stream.Length]; await stream.ReadAsync(bytes, 0, bytes.Length); for (int i = 0; i < bytes.Length; i++) { ++bytes[i]; } var fsw = new FileStream(savepath, FileMode.Create, FileAccess.Write); fsw.Write(bytes, 0, bytes.Length); fsw.Close(); } catch (Exception ex) { result.Message = ex.Message; result.Success = false; } result.Data = filepath; return result; } /// /// 远程上传文件 /// /// /// /// /// /// /// /// public async Task RemoteUploadAsync(IFormFile file, string url, string rootpath, bool fixedpath, string filename, string currentpath) { var result = new UploadResult(); var dic = new Dictionary(); dic.Add("rootpath", rootpath); dic.Add("fixedpath", fixedpath.ToString().ToLower()); dic.Add("filename", filename); dic.Add("currentpath", currentpath); using (var client = new HttpClient()) { using (var content = new MultipartFormDataContent()) { content.Add(new StreamContent(file.OpenReadStream()), "file", file.FileName); foreach (var item in dic) { content.Add(new StringContent(item.Value), item.Key); } var response = await client.PostAsync(url, content); if (response.StatusCode == HttpStatusCode.OK) { var uploadresult = await response.Content.ReadAsStringAsync(); result = uploadresult.FromJson(); } else { //result.Code = UploadCodes.Failure; //result.Message = "remote server is error"; } } } return result; } } }