示例源碼下載地址
https://qcloud.coding.net/api/project/3915794/files/4463836/download
項目地址 https://dev.tencent.com/u/whuanle/p/asp.netcore_file_upload/attachment
創建應用程序
打開VS 2017
--新建 ASP.NET Core Web 應用程序
--Web 應用程序(模型視圖控制器)
程序名字、路徑,默認即可
刪除不必要的內容
-
打開 HomeController.cs 文件,刪除所有方法
-
打開 Views/Home目錄,刪除所有文件
-
在應用程序中 新建 file 目錄
開始編程
那么,現在來寫程序,實現文件上傳
第一步 文件上傳界面
在 HomeController 中新建一個方法
這個 Action 是上傳文件的界面
public IActionResult Upload() { return View(); }
然后在 Views/Home 目錄中添加一個視圖 Upload.cshtml
把以下代碼復制到 Upload.cshtml 中
這部分就是一個文件上傳表單,沒有什么特殊的,這里不解釋代碼作用。
@{ ViewData["Title"] = "Upload"; } <form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="UploadFiles"> <div class="form-group"> <div class="col-md-12"> <p>選擇要上傳的文件</p> <input type="file" name="files" multiple /> </div> </div> <div class="form-group"> <div class="col-md-12"> <input type="submit" value="上傳" /> </div> </div> </form>
附
第二步 文件上傳功能
打開 HomeController
頭部的引用如下
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks;
在 HomeController 類里面添加一個方法
[HttpPost] //上傳文件是 post 方式,這里加不加都可以 public async Task<IActionResult> UploadFiles(List<IFormFile> files) { long size = files.Sum(f => f.Length); //統計所有文件的大小 var filepath = Directory.GetCurrentDirectory() + "\\file"; //存儲文件的路徑 ViewBag.log = "日志內容為:"; //記錄日志內容 foreach (var item in files) //上傳選定的文件列表 { if (item.Length > 0) //文件大小 0 才上傳 { var thispath = filepath + "\\" + item.FileName; //當前上傳文件應存放的位置 if (System.IO.File.Exists(thispath) == true) //如果文件已經存在,跳過此文件的上傳 { ViewBag.log += "\r\n文件已存在:" + thispath.ToString(); continue; } //上傳文件 using (var stream = new FileStream(thispath, FileMode.Create)) //創建特定名稱的文件流 { try { await item.CopyToAsync(stream); //上傳文件 } catch (Exception ex) //上傳異常處理 { ViewBag.log += "\r\n" + ex.ToString(); } } } } return View(); }
注:IFormFile 的用法將在后面介紹
貼出一張結構圖
在 Views/Home 目錄中,新建一個視圖 UploadFiles.cshtml
打開 UploadFiles.cshtml
把以下代碼放進去
下面代碼是輸出 file目錄下的文件,並輸出 日志記錄
@using System.IO @{ ViewData["Title"] = "UploadFiles"; } <h2>目錄內容</h2> <ul class="list-group"> //razor語法 輸出file目錄的文件 @{ var items = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\file"); foreach (var item in items) { <li class="list-group-item">@item.ToString()</li> } } </ul> <hr /> <h2>日志內容</h2> <p> @ViewBag.log </p>
運行
按 F5 運行應用
打開
https://localhost:你的端口/Home/Upload
即可看到運行界面
請選擇體積較小的文檔文件如txt、doc、pdf,圖片等進行測試,上傳的文件不要太多
不用選擇太多、體積大文件、dll文件、可運行文件等等,不然有可能報錯。
上傳成功
上傳成功將會跳轉到 https://localhost:你的端口/Home/UploadFiles
補充說明
-
上傳重復文件后,界面會提示
-
上傳太大或太多文件,會報錯
-
IFormFile 的用法
所屬命名空間為 Microsoft.AspNetCore.Http
屬性
ContentDisposition |
獲取上載文件的原始Content-Disposition標頭。 |
ContentType | 獲取上載文件的原始Content-Type標頭。 |
FileName | 從Content-Disposition標頭中獲取文件名。 |
Headers |
獲取上傳文件的標題字典。 |
Length | 獲取文件長度,以字節為單位。 |
Name | 從Content-Disposition標頭中獲取表單字段名稱。 |
方法
CopyTo(Stream) | 將上載文件的內容復制到 |
CopyToAsync(Stream, CancellationToken) | 異步將上載文件的內容復制到 |
OpenReadStream() | 打開請求流以讀取上載的文件。 |