写了一个.net core上传的公用类调用
using System;using System.Collections.Generic;using System.Configuration;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LiveFactory.Core{ public enum FileType { //////图片 /// image = 1, ////// 视频 /// video = 2 } public class FileInfoReulst { public FileInfoReulst() { } public FileInfoReulst(string meg) { this.Success = false; this.Message = meg; } ////// 是否成功 /// public bool Success { get; set; } = true; ////// 文件类型异常消息 /// public string Message { get; set; } ////// 保存的文件名 /// public string FileName { get; set; } ////// 1=image,2=video /// public FileType FileType { get; set; } } public class SaveFileReulst { public string SaveDriveFileName { get; set; } public string SaveFileName { get; set; } public string SaveDirectory { get; set; } } public static class FilesBaseHelper { ////// 保存的盘符根目录 /// public readonly static string DriveFolder = "D:\\Uploads\\"; ////// 文件根目录名称 /// public readonly static string BasePath = "/Uploads/"; public readonly static string ImgFileLength ="5120"; public readonly static string ImgExts = ".jpg.jpeg.png.gif"; public readonly static string VideoFileLength = "1048576"; public readonly static string VideoExts = ".mp4.avi.rmvb.mkv.flv"; const string ImageFileName = "images/"; const string VideoFileName = "video/"; static string GetFileTypeFolder(FileType filetype, bool IsBase = false) { string _path = IsBase ? BasePath : DriveFolder; switch (filetype) { case FileType.image: _path += ImageFileName; break; case FileType.video: _path += VideoFileName; break; } return _path; } public static SaveFileReulst GetSaveFilePath(FileType fileType, string fileName) { string _folder = DateTime.Now.ToString("yyyy-MM") + "/"; string driveFolder = GetFileTypeFolder(fileType) + _folder; string baseFolder = GetFileTypeFolder(fileType, true) + _folder; string bas = Directory.GetCurrentDirectory() + "/wwwroot";//获取服务器目录 if (!Directory.Exists(bas + baseFolder)) { Directory.CreateDirectory(bas + baseFolder); } //Abp.IO.DirectoryHelper.CreateIfNotExists(driveFolder); string savefilename = Guid.NewGuid() + GetExtensionName(fileName); SaveFileReulst result = new SaveFileReulst() { SaveDriveFileName = driveFolder + savefilename, SaveFileName = baseFolder + savefilename, SaveDirectory= bas + baseFolder + savefilename }; return result; } public static string GetExtensionName(string fileName) { return Path.GetExtension(fileName); } public static bool Validate(string fileName, long fileSize, FileType filetype, out string Error) { Error = ""; string extensValue = ""; long size = 0; switch (filetype) { case FileType.image: extensValue = ImgExts; size = Convert.ToInt32(ImgFileLength) * 1024; break; case FileType.video: extensValue = VideoExts; size = Convert.ToInt32(VideoFileLength) * 1024; break; } if (!ValidateeExtension(extensValue, fileName)) { Error = "文件类型错误"; return false; } if (!ValidateeSize(size, fileSize)) { Error = "文件大小不可超过" + size / 1024 + "KB"; return false; } return true; } static bool ValidateeExtension(string ExtensionValue, string fileName) { string ext = GetExtensionName(fileName).ToLower(); return ExtensionValue.ToLower().Contains(ext); } static bool ValidateeSize(long Size, long fileSize) { return fileSize <= Size; } }}
公用控制器
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using JFJT.Framework.Application.Dto;using LiveFactory.Application;using Microsoft.AspNetCore.Mvc;using LiveFactory.Core;using Microsoft.AspNetCore.Http;using System.IO;namespace LiveFactory.Web.Controllers{ public class UploadFileController : Controller { //HttpPostedFileBase public JsonResult Index(IFormFile file, FileType filetype) { var validate = FilesBaseHelper.Validate(file.FileName, file.Length, filetype, out string error); if (!validate) { throw new Exception(error); } try { var saveResult = FilesBaseHelper.GetSaveFilePath(filetype, file.FileName); using (FileStream fs = System.IO.File.Create(saveResult.SaveDirectory)) { file.CopyTo(fs); fs.Flush(); } return Json(new FileInfoReulst() { FileName = saveResult.SaveFileName,FileType= filetype }); } catch (Exception ex) { throw new Exception(error); } } }}
前端调用,这个可以不看,直接请求上面的控制器就行,ps:饿了么的上传控件,百度的webupload也一样
@* @* *@ 查看 点击上传 请上传视频文件*@