博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net core上传
阅读量:5173 次
发布时间:2019-06-13

本文共 6431 字,大约阅读时间需要 21 分钟。

写了一个.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;        }    }}
View Code

公用控制器

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);            }        }    }}
View Code

前端调用,这个可以不看,直接请求上面的控制器就行,ps:饿了么的上传控件,百度的webupload也一样

@*
*@
查看
点击上传
请上传视频文件
@*
*@
取 消
确 定
View Code

 

转载于:https://www.cnblogs.com/Cein/p/8994601.html

你可能感兴趣的文章
ucos在s3c2410上运行过程整体剖析之基础知识-与UCOS运行有关的ARM9芯片知识--续 ...
查看>>
存储器的寻址问题 分类: 计算机组成原理 2011-...
查看>>
DDRmenu(翻译)
查看>>
atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7
查看>>
Atitit 架构的原则attilax总结
查看>>
和讯网包容且务实
查看>>
ASP.Net之数据绑定
查看>>
Android自动化测试第三季第二讲Toast控件文字获取
查看>>
Google Analytics的能与不能
查看>>
Ubuntu 基本操作
查看>>
JAVA数组的定义及用法
查看>>
18寒假第七测
查看>>
帧中继
查看>>
105:MyBatis常见实用面试题整理
查看>>
Base on QC Automation Framework v1.0
查看>>
bzoj 3261: 最大异或和 (可持久化trie树)
查看>>
UVA 11440 Help Tomisu
查看>>
bzoj千题计划258:bzoj3123: [Sdoi2013]森林
查看>>
开博@纪念
查看>>
linux的正则表达式
查看>>