文件上传

浪淘沙2年前后端234
@page
@model xxtsoft.Pages.uploadModel
@{
  
}

<h1>@ViewData["Title"]</h1>

<form method="post" enctype="multipart/form-data" >
    <div class="form-group">
        <div class="col-md-12">           
            <input type="file" name="files" multiple />
            <input asp-page-handler="Upload" class="btn" type="submit" value="上传" />
        </div>
    </div>   
</form>
@Html.Raw(Model.msgJs)
using Furion;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using xxtsoft.Common;

namespace xxtsoft.Pages
{
    public class uploadModel : PageModel
    {
        public string msgJs;//js上传说明
        public string foldname;//文传上传目录
      

        public void OnGet()
        {
            foldname =FunHelper.FilerForm(Request.Query["foldname"]);//过滤非法字符
        }

        //上传文件是 post 方式,这里加不加都可以
        public async Task<IActionResult> OnPostUploadAsync(List<IFormFile> files)
        {
            
            long size = files.Sum(f => f.Length);       //统计所有文件的大小

            var filepath = App.WebHostEnvironment.WebRootPath + "/upLoadfiels/";

          

            foreach (var item in files)     //上传选定的文件列表
            {
                if (item.Length > 0)        //文件大小 0 才上传
                {
                    var thispath = Path.Combine(filepath, RandomHelper.GetGuid() + Path.GetExtension(item.FileName));     //当前上传文件应存放的位置item.FileName

                    if (System.IO.File.Exists(thispath) == true)        //如果文件已经存在,跳过此文件的上传
                    {
                        //ViewBag.log += "\r\n文件已存在:" + thispath.ToString();
                        continue;
                    }
                    //目录不存在,创建目录

                    if (!Directory.Exists(filepath))
                    {

                        Directory.CreateDirectory(filepath);
                    }
                    //上传文件
                    using (var stream = new FileStream(thispath, FileMode.Create))      //创建特定名称的文件流
                    {
                        try
                        {
                            await item.CopyToAsync(stream);     //上传文件
                        }
                        catch (Exception ex)        //上传异常处理
                        {
                            throw new Exception(ex.ToString());
                        }
                    }
                }
            }

            //msg = "<script>alert('test');</script>";



            // Process uploaded files
            // Don't rely on or trust the FileName property without validation.

            return Page();
        }
    }
}


相关文章

C#JNTemplate模板引擎,标签指定${set(pagetotal=20)},后端如何获取pagetotal的值

using JinianNet.JNTemplate; class Program {     static void&...

asp.net core razor .cshtml.cs 如何改变写入中文后变成ANSI格式为utf-8

根目录下,添加.editorconfig并写入[*.cshtml.cs] charset = utf-8已生成的文件另存为 utf-8格式...

visual studio 配置 asp.net core razor .shtml文件类型和包含,支持跨平台通用

自己在 ASP.NET Core 里写 Middleware 解析 SSI自己在 ASP.NET Core 里写 Middleware 解析 ...

asp.net core razor和api 都同时支持登录身份判断属性写法

using Microsoft.AspNetCore.Mvc.Filters;namespace xxtsoft.Application.Filters{   ...

点击按钮,禁用提示,加导出word

@page @model xxtsoft.Web.Entry.Pages.sysadmins.kh.kh_outModel @{     Lay...

asp.net core ZipDeploy iis发布 忽略某一件文件夹被删除

  services.AddZipDeploy(o => o.IgnorePathStarting("wwwroot/")); //iis发布...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。