写了一个.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 { /// <summary> ///图片 /// </summary> image = 1, /// <summary> /// 视频 /// </summary> video = 2 } public class FileInfoReulst { public FileInfoReulst() { } public FileInfoReulst(string meg) { this.Success = false; this.Message = meg; } /// <summary> /// 是否成功 /// </summary> public bool Success { get; set; } = true; /// <summary> /// 文件类型异常消息 /// </summary> public string Message { get; set; } /// <summary> /// 保存的文件名 /// </summary> public string FileName { get; set; } /// <summary> /// 1=image,2=video /// </summary> 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 { /// <summary> /// 保存的盘符根目录 /// </summary> public readonly static string DriveFolder = "D:\Uploads\"; /// <summary> /// 文件根目录名称 /// </summary> 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也一样
<el-dialog title="编辑直播频道" :visible.sync="dialogVisible" width="40%"> <el-form ref="SelectModel" :model="SelectModel" label-width="80px"> <el-form-item label="名称"> <el-input v-model="SelectModel.name"></el-input> </el-form-item> <el-form-item label="视频文件"> <el-input v-model="SelectModel.url"></el-input> @*<input type="text" name="VideoUrl" v-model="SelectModel.videoUrl" class="form-control" width="90%" />*@ <a v-if="SelectModel.videoUrl!=''" v-bind:href="SelectModel.videoUrl" target="_blank">查看</a> <el-upload ref="uploadvideo" class="upload-demo" action="/UploadFile/Index" :data="{ filetype: '@LiveFactory.Core.FileType.video'}" :limit="1" :on-success="uploadSuccess" :on-error="uploadError"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">请上传视频文件</div> </el-upload> </el-form-item> </el-form> @*<div class="form-group"> </div>*@ <span slot="footer" class="dialog-footer"> <el-button @@click="dialogVisible = false">取 消</el-button> <el-button type="primary" @@click="submitModel">确 定</el-button> </span> </el-dialog>