C# 无法上载文件asp.net mvc

C# 无法上载文件asp.net mvc,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我正在尝试在asp.net MVC中上载文件,但问题是我无法从请求.Files[] 这是控制器端 [AllowAnonymous] [HttpPost] [ValidateAntiForgeryToken] 公共行动结果验证和签名(适用于新公司) { if(ModelState.IsValid) { 新公司=新公司(); TryUpdateModel(新公司); context.companys.Add(新公司); string token=new Hash().CalculateMD5Hash

我正在尝试在asp.net MVC中上载文件,但问题是我无法从
请求.Files[]

这是控制器端

[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果验证和签名(适用于新公司)
{
if(ModelState.IsValid)
{
新公司=新公司();
TryUpdateModel(新公司);
context.companys.Add(新公司);
string token=new Hash().CalculateMD5Hash(DateTime.Now.ToString(“yyyy-MM-dd HH:MM:ss.fff”);
if(context.companys.Any(com=>com.Email==newCompany.Email.Trim())
{
TempData[“message”]=“此电子邮件已注册”+Request.Files.Count+”;
}
其他的
{
上传文件(Request.Files[0]);
context.oneTimeAuthenticationTickets.Add(新的oneTimeAuthenticationTicket{company\u id=NewCompany.company\u id,ticket=token,status=“v”,DateOfCreation=DateTime.Now});
SaveChanges();
TempData[“message”]=new-Email(){To=newcommpany.Email,From=”hworkflowteam@workflow.com“,Subject=”此电子邮件的新公司已创建“,Body=”请按照此操作验证您的帐户并创建第一个用户“}.sendMail();
}
返回重定向操作(“登录”、“帐户”);
}
其他的
{
返回视图(“注册”,新公司);
}
}
公共无效上载文件(HttpPostedFileBase文件)
{
如果(file.ContentLength>0)
{
var fileName=Path.GetFileName(file.fileName);
var path=path.Combine(Server.MapPath(“~/App\u Data/uploads”),文件名);
file.SaveAs(路径);
}
}
这里是风景区

@model workflow.DataHolders.ForCompany
如果您的公司已注册且尚未验证,或者没有创建用户,请配置您的帐户
@如果(!ViewData.ModelState.IsValid)
{
请更正下面的错误
}
@使用(Html.BeginForm(“ValidateAndSignUp”、“Accounts”、FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationMessage(“公司名称”);
公司名称:
@Html.TextBox(“公司名称”)
@Html.ValidationMessage(“电子邮件”);
电邮:
@Html.TextBox(“电子邮件”)
@Html.ValidationMessage(“国家”);
国家名称:
@Html.DropDownList(“国家”,Model.Countries)
关于公司:
@Html.TextArea(“说明”)
选择公司标志
@TextBox(“Logo”,Model.Logo,new{type=“file”})
}
与客户建立联系
打造您的专业商业世界
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
这是我使用的POCO

公司的公共类:数据持有者
{
[必需]
[StringLength(200,MinimumLength=3,ErrorMessage=“公司名称的长度应超过三个字母”)]
公共字符串CompanyName{get;set;}
[必需]
[电子邮件地址(ErrorMessage=“无效电子邮件地址”)]
公共字符串电子邮件{get;set;}
[必需]
公共整数国家{get;set;}
公共字符串说明{get;set;}
公共字符串标志{get;set;}
公共列表国家{get;set;}
}

但是,当我尝试上载文件时,会出现
超出范围的异常,因此请任何人帮助我解决此问题。

您只需像这样将
enctype=“multipart/form data”
添加到您的begin表单中即可

@using (Html.BeginForm("ValidateAndSignUp", "Accounts", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    ...

您缺少的一个主要步骤是添加enctype。比如:@using(Html.BeginForm(“ValidateAndSignUp”,“Accounts”,FormMethod.Post,new{enctype=“multipart/form data”}))可能是@Mehmood的重复,非常感谢,我怎么会忘记这样的事情:哦,非常感谢,请添加您的评论作为接受它的答案。不,不,问题是因为我没有添加
enctype
@model workflow.DataHolders.ForCompany
<link href="@Url.Content("~/sharedfiles/css/forms/addnew.css")" rel="stylesheet" type="text/css" />

<div id="Add_container">
    <div class="message">If your company is already registered and  has not been verified or there are no users created, please <a href="@Url.getBaseUrl()/Accounts/VerifyAccount">click</a>  to  configure your account</div>

    @if (!ViewData.ModelState.IsValid)
    {
        <div id="validationMessage">Please Correct The Errors Below</div>
    }


    @using (Html.BeginForm("ValidateAndSignUp", "Accounts", FormMethod.Post))
    {
        @Html.AntiForgeryToken()

        @Html.ValidationMessage("CompanyName");
        <span class="field_title">Company Name: </span>
        @Html.TextBox("CompanyName")   

        @Html.ValidationMessage("Email");
        <span class="field_title">Email: </span>
        @Html.TextBox("Email")

        @Html.ValidationMessage("Country");
        <span class="field_title">Country Name: </span>
        @Html.DropDownList("Country", Model.Countries)

        <span class="field_title">About The Company: </span>
        @Html.TextArea("Description")

        <span class="field_title">Choose Company Logo</span>
        @Html.TextBox("Logo", Model.Logo, new { type = "file" })

        <input type="submit" value="Create New Account">


    }
</div>
<div class="get_connected_message">
    <h1>Get Connected with your Customers</h1>
</div>

<div class="get_connected_message">
    <h1>Build your profissional buisness world</h1>
</div>

<div class="clear"></div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
public class ForCompany : DataHolder
{
    [Required]
    [StringLength(200, MinimumLength = 3, ErrorMessage = "Length Of The Company Name Should  Be More Than Three Letters")]
    public string CompanyName { get; set; }

    [Required]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]
    public string Email { get; set; }

    [Required]
    public int Country { get; set; }

    public string Description { get; set; }

    public string Logo { get; set; }

    public List<SelectListItem> Countries { get; set; }

}
@using (Html.BeginForm("ValidateAndSignUp", "Accounts", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    ...