Asp.net mvc asp.net mvc控制器中的文件上载错误(';HttpRequestMessage';不包含文件的定义';

Asp.net mvc asp.net mvc控制器中的文件上载错误(';HttpRequestMessage';不包含文件的定义';,asp.net-mvc,asp.net-web-api,jsajaxfileuploader,Asp.net Mvc,Asp.net Web Api,Jsajaxfileuploader,我有一个控制器链接到一个用于上传的输入元素。在我的控制器中,我遇到了一个我不太理解的奇怪错误。严重性代码描述项目路径文件行抑制状态 错误CS1061“HttpRequestMessage”不包含的定义 “文件”和无可访问的扩展方法“文件”接受第一个 找不到类型为“HttpRequestMessage”的参数(是否缺少 使用指令或程序集 参考?)SimSentinel C:\Users\tsach\Source\Workspaces\SIMSentinelv2\Website\SimSentine

我有一个控制器链接到一个用于上传的输入元素。在我的控制器中,我遇到了一个我不太理解的奇怪错误。严重性代码描述项目路径文件行抑制状态

错误CS1061“HttpRequestMessage”不包含的定义 “文件”和无可访问的扩展方法“文件”接受第一个 找不到类型为“HttpRequestMessage”的参数(是否缺少 使用指令或程序集 参考?)SimSentinel C:\Users\tsach\Source\Workspaces\SIMSentinelv2\Website\SimSentinel\SimSentinel\Controllers C:\Users\tsach\Source\Workspaces\SIMSentinelv2\Website\SimSentinel\Controllers\BulkSMSUploadController.cs

使用系统数据;
使用System.Linq;
使用System.Web.Http;
使用System.Web.Security;
使用存储库、接口;
使用Repositories.Interfaces.dto;
使用SimSentinel.Models;
使用制度;
使用System.Text.RegularExpressions;
使用Newtonsoft.Json.Linq;
使用Newtonsoft.Json.Schema;
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
//使用System.Web.Http.HttpPut;
命名空间SimSentinel.Controllers
{
[System.Web.Http.Authorize]
公共类BulkSMSUploadController:ApiController
{
公共行动结果索引()
{
//ViewBag.Message=“修改此模板以跳转启动ASP.NET MVC应用程序。”;
//返回视图();
返回null;
}
[System.Web.Mvc.HttpPost]
公共操作结果上载文件()
{

如果(Request.Files.Count,则应注意以下步骤

  • 确保表单html元素具有
    enctype=“多部分/表单数据”
    所以它应该是类似于
我通常使用html助手

@using (Html.BeginForm("Index", "JobApplication", FormMethod.Post, new { @enctype = "multipart/form-data", @id = "myForm", @class = "form-horizontal" }))
    {
// your input datas
}
  • 如果您想发布图像或某些文档,除了使用请求,您应该在ViewModel中使用
    公共HttpPostedFileBase文件{get;set;}
    。然后您可以轻松地在剃须刀上使用它

    @Html.TextBoxFor(m=>m.File,新的{@type=“File”,@onchange=“SomeValidationOnClientSide(this);”})

在后端,您可以验证您的案例。在我的案例中,我只接受PDF文件

if ((from file in model.Files where file != null select file.FileName.Split('.')).Any(arr => arr[arr.Length - 1].ToLower() != "pdf"))
        {
            ModelState.AddModelError(string.Empty, "We only accept PDF files!");
            return View(model);
        }
        if (model.Files.Count() > 2)
        {
            ModelState.AddModelError(string.Empty,
                "You have exceeded maximum file upload size. You can upload maximum 2 PDF file!");
            return View(model);
        }
编辑:我看到您实现了ApiController而不是Contoller。所以我知道您正在开发WEB.API,您也应该将其添加到问号中


如果要开发ApicController,应发送字节[],并将此字节[]处理到ApicController中。

您应注意以下步骤

  • 确保表单html元素具有
    enctype=“多部分/表单数据”
    所以它应该是类似于
我通常使用html助手

@using (Html.BeginForm("Index", "JobApplication", FormMethod.Post, new { @enctype = "multipart/form-data", @id = "myForm", @class = "form-horizontal" }))
    {
// your input datas
}
  • 如果您想发布图像或某些文档,除了使用请求,您应该在ViewModel中使用
    公共HttpPostedFileBase文件{get;set;}
    。然后您可以轻松地在剃须刀上使用它

    @Html.TextBoxFor(m=>m.File,新的{@type=“File”,@onchange=“SomeValidationOnClientSide(this);”})

在后端,您可以验证您的案例。在我的案例中,我只接受PDF文件

if ((from file in model.Files where file != null select file.FileName.Split('.')).Any(arr => arr[arr.Length - 1].ToLower() != "pdf"))
        {
            ModelState.AddModelError(string.Empty, "We only accept PDF files!");
            return View(model);
        }
        if (model.Files.Count() > 2)
        {
            ModelState.AddModelError(string.Empty,
                "You have exceeded maximum file upload size. You can upload maximum 2 PDF file!");
            return View(model);
        }
编辑:我看到您实现了ApiController而不是Contoller。所以我知道您正在开发WEB.API,您也应该将其添加到问号中


如果您想开发ApicController,您应该发送字节[],并将此字节[]处理到您的ApicController中。

从我看到的情况来看,您的控制器实现了Web API控制器(即使用),其定义如下所示:

public System.Net.Http.HttpRequestMessage Request { get; set; }
返回类型是没有
文件
属性的类型,与下面作为属性返回类型实现的类型不同:

public System.Web.HttpRequestBase Request { get; }
要解决此问题,您需要从
System.Web.Mvc.Controller
基类继承,并将Web API请求移动到继承
ApiController
的另一个类,因为您不能在同一个类上同时继承
System.Web.Mvc.Controller
System.Web.Http.ApiController

namespace SimSentinel.Controllers
{
   public class BulkSMSUploadController : Controller
   {
      [System.Web.Mvc.HttpPost]
      public ActionResult UploadFiles()
      {
         if (Request.Files.Count <= 0)
         {
            return Json("No files selected.");
         }
         else
         {
            try
            {
               HttpFileCollectionBase files = Request.Files;
               for (int i = 0; i < files.Count; i++)
               {
                  string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";
                  string filename = Path.GetFileName(Request.Files[i].FileName);

                  HttpPostedFileBase file = files[i];
                  string fname;
                  if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
                  {
                     string[] testfiles = file.FileName.Split(new char[] { '\\' });
                     fname = testfiles[testfiles.Length - 1];
                  }
                  else
                  {
                     fname = file.FileName;
                  }

                  fname = Path.Combine(Server.MapPath("~/Uploads/"), fname);
                  file.SaveAs(fname);
               }

               return Json("File Uploaded Successfully!");
            }
            catch (Exception ex)
            {
               return Json("Error occurred. Error details: " + ex.Message);
            }
         }
      }
   }

   [System.Web.Http.Authorize]
   public class BulkSMSUploadWebApiController : ApiController
   {
       public IHttpActionResult Index()
       {
           return null; 
       }
   }
}
namespace-SimSentinel.Controllers
{
公共类BulkSMSUploadController:控制器
{
[System.Web.Mvc.HttpPost]
公共操作结果上载文件()
{

如果(Request.Files.Count从我看到您的控制器实现了Web API控制器(即使用),其定义如下所示:

public System.Net.Http.HttpRequestMessage Request { get; set; }
返回类型是没有
文件
属性的类型,与下面作为属性返回类型实现的类型不同:

public System.Web.HttpRequestBase Request { get; }
要解决此问题,您需要从
System.Web.Mvc.Controller
基类继承,并将Web API请求移动到继承
ApiController
的另一个类,因为您不能在同一个类上同时继承
System.Web.Mvc.Controller
System.Web.Http.ApiController

namespace SimSentinel.Controllers
{
   public class BulkSMSUploadController : Controller
   {
      [System.Web.Mvc.HttpPost]
      public ActionResult UploadFiles()
      {
         if (Request.Files.Count <= 0)
         {
            return Json("No files selected.");
         }
         else
         {
            try
            {
               HttpFileCollectionBase files = Request.Files;
               for (int i = 0; i < files.Count; i++)
               {
                  string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";
                  string filename = Path.GetFileName(Request.Files[i].FileName);

                  HttpPostedFileBase file = files[i];
                  string fname;
                  if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
                  {
                     string[] testfiles = file.FileName.Split(new char[] { '\\' });
                     fname = testfiles[testfiles.Length - 1];
                  }
                  else
                  {
                     fname = file.FileName;
                  }

                  fname = Path.Combine(Server.MapPath("~/Uploads/"), fname);
                  file.SaveAs(fname);
               }

               return Json("File Uploaded Successfully!");
            }
            catch (Exception ex)
            {
               return Json("Error occurred. Error details: " + ex.Message);
            }
         }
      }
   }

   [System.Web.Http.Authorize]
   public class BulkSMSUploadWebApiController : ApiController
   {
       public IHttpActionResult Index()
       {
           return null; 
       }
   }
}
namespace-SimSentinel.Controllers
{
公共类BulkSMSUploadController:控制器
{
[System.Web.Mvc.HttpPost]
公共操作结果上载文件()
{

如果(Request.Files.Count谢谢大家的帮助我不得不调整控制器中的代码以使其正常工作。因此,该文件现在正在保存,但我遇到了两个额外的问题。该文件使用一些随机字符串保存,如BodyPart_2ea18b56-0c11-41f6-81ff-204bb377cbbf,保存后重定向到实际控制器本身,这是一个问题SPA应用程序中存在lem,因为这意味着用户每次上传文件后都必须再次登录。我现在将更新我的帖子,向您展示我的新控制器是什么样子的
2ea18b56-0c11-41f6-81ff-204bb377cbbf
看起来像一个GUID表单