文件上传

浪淘沙2年前后端268
@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();
        }
    }
}


相关文章

ubuntu 删除asp.net core 8.0

1. 查找已安装的 ASP.NET Core 8.0 版本dotnet --list-runtimes dotnet --list-sdks2. 删除 ASP.NET Core...

asp.net core razor 远程请求 认证Basic auth 模式 body传递 json

private readonly HttpClient _httpClient;    public RemoteReq...

Aspose.Words 插入图片

        #region 创建 Word 文档   &...

asp.net core HttpClient 返回值

  public async Task<IActionResult> OnPostDown()   {  ...

asp.net core弹出对话框最佳实践

 public void OnGet()  {         ...

发表评论    

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