C# 如何将文件上载到服务器?

C# 如何将文件上载到服务器?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在尝试使该项目适应Asp.Net框架 问题。 1.如何调整这个片段,使您不需要重做代码的其余部分? 好吧,或者要求最低限度的改动 说明。 我使用: ) 我的项目控制员 using System.Linq; using System.Web; using System.Web.Mvc; // using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.IO; u

我正在尝试使该项目适应Asp.Net框架

问题。
1.如何调整这个片段,使您不需要重做代码的其余部分? 好吧,或者要求最低限度的改动

说明。
我使用:

  • )
我的项目控制员

using System.Linq;
using System.Web;
using System.Web.Mvc;

// 
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;

// using Microsoft.

namespace WebAppFrm.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult OnPostImport()
        {
            IFormFile file = Request.Form.Files[0];
            string folderName = "Upload";
            string webRootPath = _hostingEnvironment.WebRootPath;
            string newPath = Path.Combine(webRootPath, folderName);

            StringBuilder sb = new StringBuilder();

            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            if (file.Length > 0)
            {
                string sFileExtension = Path.GetExtension(file.FileName).ToLower();
                ISheet sheet;
                string fullPath = Path.Combine(newPath, file.FileName);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                    stream.Position = 0;
                    if (sFileExtension == ".xls")
                    {
                        HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats  
                        sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook  
                    }
                    else
                    {
                        XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format  
                        sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook   
                    }

                    IRow headerRow = sheet.GetRow(0); //Get Header Row
                    int cellCount = headerRow.LastCellNum;
                    sb.Append("<table class='table'><tr>");
                    for (int j = 0; j < cellCount; j++)
                    {
                        NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j);
                        if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
                        sb.Append("<th>" + cell.ToString() + "</th>");
                    }
                    sb.Append("</tr>");
                    sb.AppendLine("<tr>");
                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null) continue;
                        if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            if (row.GetCell(j) != null)
                                sb.Append("<td>" + row.GetCell(j).ToString() + "</td>");
                        }
                        sb.AppendLine("</tr>");
                    }
                    sb.Append("</table>");
                }
            }
            return this.Content(sb.ToString());
        }




    }
}
我发现错误:

  • “NameValueCollection”不包含“Files”的定义,并且找不到可用的扩展方法“Files”,接受类型“NameValueCollection”作为第一个参数(可能缺少使用指令或程序集引用)

  • 当前上下文中不存在名称“\u hostingEnvironment”

  • 找不到类型或命名空间名称“ifformfile”(可能缺少using指令或程序集引用)


  • 更新-1 我安装了-
    Microsoft.AspNetCore.Http

    我安装了-
    Microsoft.AspNetCore.Hosting

    左侧错误

    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    // 
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using NPOI.XSSF.UserModel;
    using System.IO;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Hosting;
    
    // using Microsoft.
    
    namespace WebAppFrm.Controllers
    {
        public class HomeController : Controller
        {
            // GET: Home
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult OnPostImport()
            {
                IFormFile file = Request.Form.Files[0];
                string folderName = "Upload";
                string webRootPath = _hostingEnvironment.WebRootPath;
                string newPath = Path.Combine(webRootPath, folderName);
    
                StringBuilder sb = new StringBuilder();
    
                if (!Directory.Exists(newPath))
                {
                    Directory.CreateDirectory(newPath);
                }
                if (file.Length > 0)
                {
                    string sFileExtension = Path.GetExtension(file.FileName).ToLower();
                    ISheet sheet;
                    string fullPath = Path.Combine(newPath, file.FileName);
                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                        stream.Position = 0;
                        if (sFileExtension == ".xls")
                        {
                            HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats  
                            sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook  
                        }
                        else
                        {
                            XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format  
                            sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook   
                        }
    
                        IRow headerRow = sheet.GetRow(0); //Get Header Row
                        int cellCount = headerRow.LastCellNum;
                        sb.Append("<table class='table'><tr>");
                        for (int j = 0; j < cellCount; j++)
                        {
                            NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j);
                            if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
                            sb.Append("<th>" + cell.ToString() + "</th>");
                        }
                        sb.Append("</tr>");
                        sb.AppendLine("<tr>");
                        for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File
                        {
                            IRow row = sheet.GetRow(i);
                            if (row == null) continue;
                            if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
                            for (int j = row.FirstCellNum; j < cellCount; j++)
                            {
                                if (row.GetCell(j) != null)
                                    sb.Append("<td>" + row.GetCell(j).ToString() + "</td>");
                            }
                            sb.AppendLine("</tr>");
                        }
                        sb.Append("</table>");
                    }
                }
                return this.Content(sb.ToString());
            }
    
    
    
    
        }
    }
    
  • “NameValueCollection”不包含“文件”的定义, 并且找不到可用的扩展方法“Files”,正在接受 键入“NameValueCollection”作为第一个参数(可能使用 缺少指令或程序集引用)

  • 当前数据库中不存在名称“\u hostingEnvironment” 上下文


  • 更新2
    已安装
    System.Collections.Specialized

    按下-F6

    没有帮助


    更新3 这个问题的目的是:
    -创建可导入Excel文件的应用程序。
    我以github.com上的项目作为基础。
    在控制器中,文件下载到服务器的代码位置出现错误。

    如何修复代码,以便将文件上载到服务器

    您是否安装了此项目所需的所有软件包

    • 对于IFormFile,我们需要包含名称空间Microsoft.AspNetCore.Http

      NuGet命令
      安装软件包Microsoft.AspNetCore.Http-版本2.2.2

    • 用于托管环境类System.Web.Hosting

      NuGet命令
      安装软件包Microsoft.AspNetCore.Hosting-版本2.2.7

    希望这会有所帮助


    这是将文件复制到服务器的代码的主要部分。您没有声明object\u HostingEnvironment,因此出现了错误。您不能将.NET Core和.NET Framework结合使用。

    请参阅更新-1我认为根本问题是“NameValueCollection”,System.Collections.Specialized包含“System.Collections.Specialized.NameValueCollection”,请安装以下软件包。\uuuuuuuuuuuuuuuuuuuuuuu安装软件包System.Collections.Specialized-已安装版本4.3.0
    System.Collections.Specialized
    。按下-F6。没有帮助。我不知道你想做什么。你能再解释一下吗?是否要采用从.NET Core到.NET的代码?@GlacialMan此问题的目的:-创建一个可以导入Excel文件的应用程序。我以github.com上的项目为基础。在控制器中,我得到错误。如何修复代码,以便将文件上载到服务器?我需要解决一个上传文件到服务器的问题。