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

  • 相关阅读:
    李永乐说股票
    01 什么是基金
    基金分享
    02 基金分类
    03 买什么基金合适
    06丨MongoDB基本操作
    详解大端模式和小端模式
    在UIViewController中获得Container View里的embed viewController的引用
    iphone Dev 开发实例10:How To Add a Slide-out Sidebar Menu in Your Apps
    iphone Dev 开发实例9:Create Grid Layout Using UICollectionView in iOS 6
  • 原文地址:https://www.cnblogs.com/ataoliu/p/13387464.html
Copyright © 2011-2022 走看看