添加文件操作

This commit is contained in:
橙子
2022-05-03 19:40:13 +08:00
parent 5eec076ea2
commit b934ce2893
12 changed files with 135 additions and 8 deletions

Binary file not shown.

View File

@@ -90,6 +90,34 @@
<param name="ids"></param>
<returns></returns>
</member>
<member name="T:Yi.Framework.ApiMicroservice.Controllers.FileController">
<summary>
文件
</summary>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.FileController.#ctor(Yi.Framework.Interface.IUserService,Microsoft.Extensions.Hosting.IHostEnvironment)">
<summary>
使用本地存储,未进行数据库记录
</summary>
<param name="iUserService"></param>
<param name="env"></param>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.FileController.Get(System.String,System.String)">
<summary>
文件下载
</summary>
<param name="type"></param>
<param name="fileName"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.FileController.Upload(System.String,Microsoft.AspNetCore.Http.IFormFile)">
<summary>
文件上传
</summary>
<param name="type"></param>
<param name="file"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.LogController.Add">
<summary>
自动分表,日志添加

View File

@@ -0,0 +1,95 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
using Yi.Framework.Interface;
using Yi.Framework.WebCore;
namespace Yi.Framework.ApiMicroservice.Controllers
{
/// <summary>
/// 文件
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class FileController : ControllerBase
{
private IUserService _iUserService;
private readonly IHostEnvironment _env;
/// <summary>
/// 使用本地存储,未进行数据库记录
/// </summary>
/// <param name="iUserService"></param>
/// <param name="env"></param>
public FileController(IUserService iUserService, IHostEnvironment env)
{
_iUserService = iUserService;
_env = env;
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="type"></param>
/// <param name="fileName"></param>
/// <returns></returns>
[Route("/api/{type}/{fileName}")]
[HttpGet]
public IActionResult Get(string type, string fileName)
{
try
{
var path = Path.Combine($"wwwroot/{type}", fileName);
var stream = System.IO.File.OpenRead(path);
var MimeType = Common.Helper.MimeHelper.GetMimeMapping(fileName);
return new FileStreamResult(stream, MimeType);
}
catch
{
return new NotFoundResult();
}
}
/// <summary>
/// 文件上传
/// </summary>
/// <param name="type"></param>
/// <param name="file"></param>
/// <returns></returns>
[Route("/api/Upload/{type}")]
[HttpPost]
public async Task<Result> Upload(string type, IFormFile file)
{
try
{
string filename = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
using (var stream = new FileStream(Path.Combine($"wwwroot/{type}", filename), FileMode.CreateNew, FileAccess.Write))
{
await file.CopyToAsync(stream);
}
return Result.Success().SetData(filename);
}
catch
{
return Result.Error();
}
}
//[HttpGet]
//public async Task<IActionResult> ExportFile()
//{
// var userdata = await _userService.GetAllEntitiesTrueAsync();
// var userList = userdata.ToList();
// List<string> header = new() { "用户", "密码", "头像", "昵称", "邮箱", "ip", "年龄", "个人介绍", "地址", "手机", "角色" };
// var filename = Common.Helper.ExcelHelper.CreateExcelFromList(userList, header, _env.ContentRootPath.ToString());
// var MimeType = Common.Helper.MimeHelper.GetMimeMapping(filename);
// return new FileStreamResult(new FileStream(Path.Combine(_env.ContentRootPath+@"/wwwroot/excel", filename), FileMode.Open),MimeType);
//}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@@ -78,7 +78,7 @@ namespace Yi.Framework.Model.Models
///
///</summary>
[SugarColumn(ColumnName="Icon" )]
public long? Icon { get; set; }
public string Icon { get; set; }
/// <summary>
///
///</summary>

View File

@@ -1,8 +1,8 @@
import myaxios from '@/util/myaxios'
export default {
EditIcon(file) {
UploadImage(file) {
return myaxios({
url: '/File/EditIcon',
url: '/Upload/image',
method: 'post',
headers: { "Content-Type": "multipart/form-data" },
data: file

View File

@@ -38,6 +38,7 @@ const mutations = { //变化//载荷
},
SET_USER(state, user) {
state.user = user
console.log(user)
setUser(user)
},
SetGradient(state, gradient) {
@@ -50,7 +51,7 @@ const mutations = { //变化//载荷
//在action中可以配合axios进行权限判断
const actions = { //动作
setIcon({ commit, state }, icon) {
SetIcon({ commit, state }, icon) {
state.user.icon = icon
commit('SET_USER', state.user)
},

View File

@@ -272,7 +272,6 @@
<script>
import fileApi from "../api/fileApi";
import userApi from "../api/userApi";
import accountApi from "../api/accountApi";
export default {
name: "UserProfileView",
@@ -343,15 +342,19 @@ export default {
this.$refs.imgFile.dispatchEvent(new MouseEvent("click"));
},
uploadImage() {
//修改头像需要先上传头像修改editInfo的头像信息即可
const file = this.$refs.imgFile.files[0];
let formData = new FormData();
formData.append("file", file);
fileApi.EditIcon(formData).then((resp) => {
this.init();
this.$dialog.notify.success(resp.msg, {
fileApi.UploadImage(formData).then((resp) => {
this.editInfo.icon=resp.data
this.$dialog.notify.success("头像加载成功,点击保存以设置", {
position: "top-right",
timeout: 5000,
});
this.$store.dispatch("SetIcon", this.editInfo.icon)
});
},
},