快捷搜索:  汽车  科技

netcore怎么修改文件内容(.netcore进行文件上传)

netcore怎么修改文件内容(.netcore进行文件上传)下面是视图的代码在这里我没有写上传文件存放的代码 为了方便省事主要是后台能接收到文件就好 自己写方法保存控制器里面写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 : Contro


netcore怎么修改文件内容(.netcore进行文件上传)(1)


.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"; } <h1>文件上传</h1> <form enctype="multipart/form-data" asp-controller="Home" asp-action="UpLoad" method="post"> <div class="form-group"> <div> <p>选择要上传的文件</p> <input type="file" name="file" value="" multiple /> </div> </div> <div class="form-group"> <div class="col-md-12"> <input type="submit" value="上传" /> </div> </div> </form>

视图这里要注意一下

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

猜您喜欢: