.net  core 和.net framework上传文件还是有一些区别的有很多注意的地方

.net framework 上传文件用httppostedfilebase

.net core 上传文件用 IFormFile

下面废话不多说了,直接上代码

控制器里面写

using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Logging;using CoreUpLoad.Models;using Microsoft.AspNetCore.Http;using System.IO;namespace CoreUpLoad.Controllers{ public class HomeController : Controller {  public IActionResult Index()  {   return View();  }  [HttpPost]  public IActionResult UpLoad(IFormFile file)  {   return View();  } }}

index 作为上传的视图页面

UpLoad  作为接受上传的方法

在这里我没有写上传文件存放的代码,为了方便省事主要是后台能接受到文件就好,自己写方法保存,

下面是视图的代码

@{ ViewData["Title"] = "Index";}

文件上传

选择要上传的文件

视图这里要注意一下,


input的name属性必须要和控制器里穿的参数名一样,我这里写的都是file
multiple 属性能够接受多个文件上传,要是上传单个文件就不需要写

 

C# .net Core 文件上传
文章转载:http://www.shaoqun.com/a/463645.html