濮阳杆衣贸易有限公司

主頁 > 知識庫 > asp.net core實現(xiàn)文件上傳功能

asp.net core實現(xiàn)文件上傳功能

熱門標(biāo)簽:400電話辦理怎么樣 西寧呼叫中心外呼系統(tǒng)線路商 外呼電話機(jī)器人成本 網(wǎng)絡(luò)電話外呼系統(tǒng)上海 蘇州如何辦理400電話 地圖標(biāo)注軟件免費(fèi)下載 百應(yīng)電話機(jī)器人外呼系統(tǒng) 臨沂智能電話機(jī)器人加盟 聯(lián)通官網(wǎng)400電話辦理

本文實例為大家分享了單文件上傳、多文件上傳的功能,供大家參考,具體內(nèi)容如下

單文件上傳
 上傳文件在Web應(yīng)用程序中是一個常見的功能。在asp.net core中上傳文件并保存在服務(wù)器上,是很容易的。下面就來演示一下怎么樣在 ASP.NET Core項目中進(jìn)行文件上傳。
 首先,創(chuàng)建一個 asp.net core 項目,然后在Controller文件件添加一個HomeController,然后在 Views 文件夾的 Home 文件夾里添加一個 New.cshtml 視圖文件。如下圖: 

添加一個 UserViewModel.cs在 Model 文件夾中 , 代碼如下:

 public class UserViewModel
{
  [Required]
  [Display(Name = "姓名")]
  public string Name { get; set; }

  [Required]
  [Display(Name = "身份證")]
  [RegularExpression(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$", ErrorMessage = "身份證號不合法")]
  public string IdNum { get; set; }

  public string IdCardImgName { get; set; }

  [Required]
  [Display(Name = "身份證附件")]
  [FileExtensions(Extensions = ".jpg,.png", ErrorMessage = "圖片格式錯誤")]
  public IFormFile IdCardImg { get; set; }
} 

然后添加一個 New.cshtml 視圖文件在 Views 文件夾中:

 @model UserViewModel

form asp-controller="Home" role="form" asp-action="New" enctype="multipart/form-data" method="post">
  div class="form-group">
    label asp-for="Name">/label>
    input type="text" class="form-control" asp-for="Name" />
  /div>
  div class="form-group">
    label asp-for="IdNum">/label>
    input type="text" class="form-control" asp-for="IdNum" />
  /div>
  div class="form-group">
    label asp-for="IdCardImg">/label>
    input type="file" asp-for="IdCardImg" />
    p class="help-block">上傳。/p>
  /div>
  button type="submit" class="btn btn-default">提交/button>
/form> 

在 HomeController 中,添加頁面對應(yīng)的 Action 方法:

 [HttpPost]
public IActionResult New([FromServices]IHostingEnvironment env, [FromServices]AppDbContext dbContext, UserViewModel user) {
  var fileName = Path.Combine("upload", DateTime.Now.ToString("MMddHHmmss") + ".jpg");
  using (var stream = new FileStream(Path.Combine(env.WebRootPath, fileName), FileMode.CreateNew)) {
    user.IdCardImg.CopyTo(stream);
  }

  var users = dbContext.SetUser>();
  var dbUser = new User() {
    Name = user.Name,
    IdCardNum = user.IdNum,
    IdCardImgName = fileName
  };
  users.Add(dbUser);
  dbContext.SaveChanges();

  return RedirectToAction(nameof(Index));
} 

運(yùn)行程序,查看表單: 


多文件上傳

多文件上傳和單文件上傳類似,表單的 ViewModel 使用 ICollectionIFromFile> ,然后表單的input type="file" asp-for="IdCardImg" mulpitle /> 添加上mulpitle就可以了(只支持 H5)。 

示例源碼
 注:示例數(shù)據(jù)存儲使用的 Sqlite ,Code First方式生成數(shù)據(jù)庫。
 示例代碼已經(jīng)上傳至 github: https://github.com/yuleyule66/AspNetCoreFileUpload

本文地址:http://www.cnblogs.com/savorboard/p/5599563.html
 作者博客:Savorboard

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • ASP.NET Core文件上傳與下載實例(多種上傳方式)
  • asp.net core分塊上傳文件示例
  • asp.net core mvc實現(xiàn)文件上傳實例
  • asp.net core webapi文件上傳功能的實現(xiàn)

標(biāo)簽:清遠(yuǎn) 甘肅 中衛(wèi) 慶陽 聊城 海西 臨夏

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net core實現(xiàn)文件上傳功能》,本文關(guān)鍵詞  asp.net,core,實現(xiàn),文件,上傳,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net core實現(xiàn)文件上傳功能》相關(guān)的同類信息!
  • 本頁收集關(guān)于asp.net core實現(xiàn)文件上傳功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    烟台市| 岚皋县| 开化县| 五莲县| 广丰县| 九台市| 六盘水市| 田阳县| 浦城县| 改则县| 南汇区| 云龙县| 广德县| 沙雅县| 栖霞市| 昌宁县| 灵台县| 徐州市| 尉氏县| 秭归县| 余干县| 西林县| 广元市| 获嘉县| 德化县| 长岛县| 进贤县| 鸡西市| 清镇市| 衡东县| 松江区| 固原市| 丹巴县| 双峰县| 专栏| 巧家县| 新河县| 东明县| 竹溪县| 合阳县| 三河市|