ASP.NET CORE 中的 Request.Files

编程入门 行业动态 更新时间:2024-10-19 14:37:33
本文介绍了ASP.NET CORE 中的 Request.Files的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 ajax 请求使用 aspnet 核心上传文件.在以前版本的 中,我曾经使用

I am trying to upload files using aspnet core using ajax request . In previous versions of i used to handle this using

foreach (string fileName in Request.Files) { HttpPostedFileBase file = Request.Files[fileName]; //Save file content goes here fName = file.FileName; (...)

但现在它在 request.files 处显示错误我怎样才能让它工作?搜了一下发现httppostedfile已经改成了iformfile但是request.files怎么处理呢?

but now its showing error at request.files how can i get it to work ? i searched and found that httppostedfile has been changed to iformfile but how to handle request.files?

推荐答案

这是来自最近项目的工作代码.数据已从 Request.Files 移至 Request.Form.Files.如果您需要将流转换为字节数组 - 这是唯一对我有用的实现.其他人会返回空数组.

This is working code from a recent project. Data has been moved from Request.Files to Request.Form.Files. In case you need to convert stream to byte array - this is the only implementation that worked for me. Others would return empty array.

using System.IO; var filePath = Path.GetTempFileName(); foreach (var formFile in Request.Form.Files) { if (formFile.Length > 0) { using (var inputStream = new FileStream(filePath, FileMode.Create)) { // read file to stream await formFile.CopyToAsync(inputStream); // stream to byte array byte[] array = new byte[inputStream.Length]; inputStream.Seek(0, SeekOrigin.Begin); inputStream.Read(array, 0, array.Length); // get file name string fName = formFile.FileName; } } }

更多推荐

ASP.NET CORE 中的 Request.Files

本文发布于:2023-11-24 22:59:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   CORE   Files   Request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!