c# - 如何将文件上传到服务器?
问题描述
我正在尝试为 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);
我收到错误:
“NameValueCollection”不包含“Files”的定义,并且找不到可用的扩展方法“Files”,接受类型“NameValueCollection”作为第一个参数(可能缺少使用指令或程序集引用)。
当前上下文中不存在名称“_hostingEnvironment”。
找不到类型或命名空间名称“IFormFile”(可能缺少 using 指令或程序集引用)。
更新 1
我安装了 - Microsoft.AspNetCore.Http
。
我安装了 - Microsoft.AspNetCore.Hosting
。
遗留错误
“NameValueCollection”不包含“Files”的定义,并且找不到可用的扩展方法“Files”,接受类型“NameValueCollection”作为第一个参数(可能缺少使用指令或程序集引用)。
当前上下文中不存在名称“_hostingEnvironment”。
更新 2
已安装System.Collections.Specialized
。
按下 - F6。
没有帮助。
更新 3
这个问题的目的:
- 创建一个可以导入 Excel 文件的应用程序。
我把这个项目github.com
作为基础。
在控制器中,我在将文件下载到服务器的代码位置出现错误。
如何修复代码以便您可以将文件上传到服务器?
解决方案
推荐阅读
- android - 在 Firefox Android 上自动播放视频(无声音且静音)
- rust - 为什么编译器不会自动在声明性宏中添加双括号?
- arrays - C - 将元素保存到数组
- css - 为什么 CSS translate3d 使用左手坐标系
- c# - 使用 MVVM 将 ObservableCollection 绑定到 ComboBox 和 DataGridComboBoxColumn
- python - Python 2.7 出现错误 TypeError: 不支持的操作数类型 /: 'datetime.timedelta' 和 'datetime.timedelta'
- python - ImportError:无法从“keras.optimizers”导入名称“rmsprop”
- c# - Wpf:ContentControl.AddChild(Object) 方法
- python - 在 beautifulsoup4 中选择 div ID
- python - Tkinter 按钮复制