文件上传

This commit is contained in:
Jimmy 2021-10-22 01:20:09 +08:00
parent 918f864763
commit 60a4bc3644
2 changed files with 64 additions and 3 deletions

View File

@ -2,11 +2,15 @@
using Abp.Application.Services.Dto;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Http;
using MineTec.ProManager.Commd;
using MineTec.ProManager.GalleryFiles.Dto;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
namespace MineTec.ProManager.GalleryFiles
{
@ -127,5 +131,62 @@ namespace MineTec.ProManager.GalleryFiles
}
return result;
}
//单个文件上传
public async Task<string> UploadFilessss(IFormFile file)
{
if (file == null)
{
return "请先选择文件";
}
string webRootPath = @"D:\UploadingFiles\"; // wwwroot 文件夹
string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd"));
string dirPath = Path.Combine(webRootPath, uploadPath);
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
string fileExt = Path.GetExtension(file.FileName).Trim('.'); //文件扩展名,不含“.”
string newFileName = Guid.NewGuid().ToString().Replace("-", "") + "." + fileExt; //随机生成新的文件名
var filePath = Path.Combine(dirPath, newFileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
var url = $@"\{uploadPath}\{newFileName}";
return url;
}
//多个文件上传
public async Task<string> UploadPhotoszzzzz(IFormFileCollection files)
{
long size = files.Sum(f => f.Length);
string webRootPath = @"D:\UploadingFiles\"; // wwwroot 文件夹
string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd"));
string fileFolder = Path.Combine(webRootPath, uploadPath);
if (!Directory.Exists(fileFolder))
Directory.CreateDirectory(fileFolder);
foreach (var file in files)
{
if (file.Length > 0)
{
var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") +
Path.GetExtension(file.FileName);
var filePath = Path.Combine(fileFolder, fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
}
var url = $@"\{uploadPath}\11";
return url;
}
}
}

View File

@ -322,7 +322,7 @@
//选完文件后不自动上传
upload.render({
elem: '#btn_annex',
url: 'https://httpbin.org/post', //此处用的是第三方的 http 请求演示,实际使用时改成您自己的上传接口即可。
url: apiUrl + '/api/services/app/GalleryFiles/UploadPhotos', //此处用的是第三方的 http 请求演示,实际使用时改成您自己的上传接口即可。
auto: false,
multiple: false,
accept: 'file', //普通文件
@ -345,9 +345,9 @@
//选完文件后不自动上传
upload.render({
elem: '#btn_card',
url: 'https://httpbin.org/post', //此处用的是第三方的 http 请求演示,实际使用时改成您自己的上传接口即可。
url: apiUrl +'/api/services/app/GalleryFiles/UploadPhotoszzzzz', //此处用的是第三方的 http 请求演示,实际使用时改成您自己的上传接口即可。
auto: false,
multiple: false,
multiple: true,
accept: 'file', //普通文件
exts: 'pdf|jpg', //只允许上传pdf和jpg
size: 102400, //限制文件大小,单位 KB