首页 > 解决方案 > 如何将文件上传到服务器?

问题描述

我正在尝试为 Asp.Net 框架调整ImportExportExcelASPNetCore // github.com项目。

问题。
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());
        }




    }
}

在代码片段中

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

我收到错误:

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

    1. 当前上下文中不存在名称“_hostingEnvironment”。

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

在此处输入图像描述


更新 1

我安装了 - Microsoft.AspNetCore.Http
我安装了 - Microsoft.AspNetCore.Hosting

遗留错误

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

  2. 当前上下文中不存在名称“_hostingEnvironment”。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述


更新 2

已安装System.Collections.Specialized
按下 - F6。

没有帮助。


更新 3

这个问题的目的:
 - 创建一个可以导入 Excel 文件的应用程序。
我把这个项目github.com作为基础。
在控制器中,我在将文件下载到服务器的代码位置出现错误。
如何修复代码以便您可以将文件上传到服务器?

标签: c#asp.netasp.net-mvc

解决方案


在此处输入图像描述

这是将文件复制到服务器的代码的主要部分。你没有声明对象 _hostingEnviroment 所以你有错误。而且您不能将 .NET Core 和 .NET Framework 结合起来。


推荐阅读