zoukankan      html  css  js  c++  java
  • C# .net Core 文件上传

    .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 属性能够接受多个文件上传,要是上传单个文件就不需要写

  • 相关阅读:
    【转】FIddler+Proxifer工具对windows PC客户端进行抓包
    Json提取器(Json Extractor)
    Json断言
    015-Zabbix自动发现和自动注册
    014-Zabbix的自动发现
    013-zabbix trapper方式监控
    012-zabbix主动模式
    011-通过安装percona插件监控MySQL
    010-监控windows主机
    009-通过jmx监控tomcat
  • 原文地址:https://www.cnblogs.com/ataoliu/p/13387464.html
Copyright © 2011-2022 走看看