【问题标题】:File Uploading In MVC 3 is always nullMVC 3 中的文件上传始终为空
【发布时间】:2012-12-06 09:07:10
【问题描述】:

我想创建一个表单,用户可以在其中编辑个人资料并根据需要上传图片, 这是我的看法

//表单字符串@using(Html.BeginForm("EditProfile","Employee",FormMethod.Post, new { enctype = "multipart/form-data " }))

//输入HTML <input type="file" name="file" id="file"/>

//提交<input type="submit" name="SubmitBtn" value="SaveProfile" /> <input type="submit" name="SubmitBtn" value="Cancel" />

现在是我的控制器

    //Profile Modification
    [Authorize]
    [HttpPost]
    public ActionResult EditProfile(employer model, string SubmitBtn,HttpFileCollection file)
    {
        switch (SubmitBtn)
        {
            case "SaveProfile":
                try
                {
                    if (ModelState.IsValid)
                    {
                          //the file is always null
                        if (file != null)
                        {
                             //Function I want to Apply on file
                             model.logoname = logouploded(file);  
                            return RedirectToAction("Profile", "Employer");
                        }
                        return RedirectToAction("EditProfile");
                    }
                    //ChangeEmployeeProfile(model);

                }
                catch (Exception ex)
                {
                    ViewBag.warn = ex.Message;
                    return View(model);
                }
                break;
            case "Cancel":
                return RedirectToAction("index");
        }
        return View(model);
    }

现在无论我上传什么文件,文件总是为空 我也尝试过将 HttpFileCollectionBase 作为 Action Function 中的参数仍然文件为空

只是提到员工模型只包含图像文件名 因为我只想将图像名称保存在数据库中。

【问题讨论】:

  • 您好,您在回帖后签入Request.Files 收藏了吗?
  • 删除 "multipart/form-data " 中的最后一个空格。正确:“multipart/form-data”

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


【解决方案1】:

将 HttpFileCollection 类型更改为 HttpPostedFileBase

【讨论】:

  • 在更正“multipart/form-data”中的空格后尝试 HttpPostedFileBase 但现在文件导致​​ modelstate.isvalide() 返回 false。
  • Message = "从类型'System.Web.HttpPostedFileWrapper'到类型'System.Web.HttpPostedFile'的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。"
  • 最后它的工作感谢帮助“multipart/form-data”中的空白是问题,参数已更改为 HttpPostedFileBase,它的工作就像魅力再次感谢@peter Kiss
猜你喜欢
  • 2013-02-03
  • 1970-01-01
  • 2021-12-23
  • 2011-11-10
  • 1970-01-01
  • 2013-04-12
  • 2015-05-29
  • 2020-01-20
  • 1970-01-01
相关资源
最近更新 更多