排名
1
文章
860
粉丝
112
评论
163
.net core自定义项目模板,创建自己的模板项目,使用命令行创建模板项目
尘叶心繁 : 可以可以讲真的我都想弄个模板
net core webapi post传递参数
庸人 :
确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 : 已精
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 :
疯狂反射
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

封装的代码如下
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace WY.JBLand.API.Tools
{
public enum UpLoadFileState { Sucess, TypeNoSupport, NoFile };
public class UpLoadFileResult
{
public string FilePath { get; set; }
public UpLoadFileState UpLoadFileState { get; set; }
}
/// <summary>
/// 文件上传工具类封装
/// </summary>
public class UpLoadFileTools
{
public UpLoadFileResult FileUpLoad(IFormFileCollection files, string[] limitTypes, string savePath)
{
UpLoadFileResult upLoadFileResult = new UpLoadFileResult();
if (files == null || files.Count == 0)
{
upLoadFileResult.UpLoadFileState = UpLoadFileState.NoFile;
return upLoadFileResult;
}
foreach (IFormFile file in files)
{
//定义图片数组后缀格式
//string[] LimitPictureType = { ".XLS" };
//获取图片后缀是否存在数组中
string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
if (!limitTypes.Contains(currentPictureExtension))
{
upLoadFileResult.UpLoadFileState = UpLoadFileState.TypeNoSupport;
return upLoadFileResult;
}
//文件重命名防止重复
var newName = Guid.NewGuid().ToString("N") + currentPictureExtension.ToLower();
var new_path = Path.Combine("uploads/files/", newName);
//判断一下存储路径是否存在,不存在就创建
//var savaDir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads/files");
var savaDir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", savePath);
if (!Directory.Exists(savaDir))
Directory.CreateDirectory(savaDir);
string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", new_path);
using (var stream = new FileStream(path, FileMode.Create))
{
file.CopyTo(stream);
stream.Flush();
upLoadFileResult.FilePath = path;
}
}
upLoadFileResult.UpLoadFileState = UpLoadFileState.Sucess;
return upLoadFileResult;
}
}
}
调用
ReturnModel<int> returnModel = new ReturnModel<int>();
UpLoadFileTools upLoadFileTools = new UpLoadFileTools();
IFormFileCollection files = Request.Form.Files;
//定义图片数组后缀格式
string[] LimitTypes = { ".XLS" };
string savePath = "uploads/files";
UpLoadFileResult upLoadFileResult = upLoadFileTools.FileUpLoad(files, LimitTypes, savePath);
if (upLoadFileResult.UpLoadFileState == UpLoadFileState.NoFile)
{
returnModel.Code = Enum.ResultCode.MESSAGE_ERROR;
returnModel.Msg = "没有上传文件,无法导入,请检查";
return returnModel;
}
if (upLoadFileResult.UpLoadFileState == UpLoadFileState.TypeNoSupport)
{
returnModel.Code = Enum.ResultCode.MESSAGE_ERROR;
returnModel.Msg = "暂时只支持excel导入,请检查";
return returnModel;
}
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价