.net core下對于附件上傳下載的實現(xiàn)示例
本篇主要介紹下文件的上傳與下載。分享給大家,具體如下:
文件上傳下載也是系統(tǒng)中常用的功能,不啰嗦,直接上代碼看下具體的實現(xiàn)。
文件上傳
.net core通過 IFormFile 接收文件對象,再通過流的方式保存至指定的地方。
[HttpPost("upload")]
//[DisableRequestSizeLimit] //禁用http限制大小
[RequestSizeLimit(100*1024*1024)] //限制http大小
public async Task<IActionResult> Post(List<IFormFile> files)
{
try
{
if (files == null || !files.Any())
return AssertNotFound(new ResponseFileResult { Result = false, Code = ResponseCode.InvalidParameters, ErrorMessage = "附件不能為空" });
string filePath = Path.Combine(Directory.GetCurrentDirectory(), BASEFILE, $@"Template");
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
var result = new ResponseFileResult();
var fileList = new List<FileResultModel>();
foreach (var file in files)
{
var fileModel = new FileResultModel();
var fileName = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
var newName = Guid.NewGuid().ToString() + Path.GetExtension(fileName);
var filefullPath = Path.Combine(filePath, $@"{newName}");
using (FileStream fs = new FileStream(filefullPath, FileMode.Create))//System.IO.File.Create(filefullPath)
{
file.CopyTo(fs);
fs.Flush();
}
fileList.Add(new FileResultModel { Name = fileName, Size = file.Length, Url = $@"/file/download?fileName={newName}" });
}
result.FileResultList = fileList;
return AssertNotFound(result);
}
catch(Exception ex)
{
return AssertNotFound(new ResponseFileResult { Result = false, Code = ResponseCode.UnknownException, ErrorMessage = ex.Message });
}
}
其中http會默認限制一定的上傳文件大小,可通過 [DisableRequestSizeLimit] 禁用http限制大小,也可通過 [RequestSizeLimit(1024)] 來指定限制http上傳的大小。
文件下載
相對于上傳,下載就比較簡單了,找到指定的文件,轉(zhuǎn)換成流,通過.net core自帶的 File 方法返回流文件,完成文件下載:
[HttpGet("download")]
public async Task<IActionResult> Get(string fileName)
{
try
{
var addrUrl = Path.Combine(Directory.GetCurrentDirectory(), BASEFILE, $@"{fileName}");
FileStream fs = new FileStream(addrUrl, FileMode.Open);
return File(fs, "application/vnd.android.package-archive", fileName);
}
catch(Exception ex)
{
return NotFound();
}
}
總結(jié)
文件的上傳下載的基本操作簡單介紹了下,大家可以嘗試下。以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解開源免費且穩(wěn)定實用的.NET PDF打印組件itextSharp(.NET組件介紹之八)
本篇文章主要介紹了.NET PDF打印組件itextSharp。.NET中實現(xiàn)PDF打印的組件比較多,例如PDFsharp、Report.NET、sharpPDF、itextSharp等等,今天主要簡單的介紹itextSharp組件。有興趣的可以了解一下。2016-12-12
解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問題
這篇文章主要介紹了解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問題的相關(guān)資料,需要的朋友可以參考下2016-09-09
.Net Core 實現(xiàn)圖片驗證碼的實現(xiàn)示例
這篇文章主要介紹了.Net Core 實現(xiàn)圖片驗證碼的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
解析WPF綁定層次結(jié)構(gòu)數(shù)據(jù)的應(yīng)用詳解
本文講述WPF中單層次數(shù)據(jù)和多層次數(shù)據(jù)的綁定方法,主要闡述數(shù)據(jù)綁定的顯示層面,其中涉及了ListBox和Treeview控件。并說明它們之間的差異2013-05-05
.net Core 3.0 WebApi 創(chuàng)建Linux守護進程的方法
這篇文章主要介紹了.net Core 3.0 WebApi 創(chuàng)建Linux守護進程的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
MongoDB.Net工具庫MongoRepository使用方法詳解
這篇文章主要為大家詳細介紹了MongoDB.Net工具庫MongoRepository的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

