加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 文件上传在MVC 3中始终为空

发布时间:2020-12-15 04:05:11 所属栏目:百科 来源:网络整理
导读:我想创建一个表单,用户可以在其中编辑个人资料并上传图像(如果需要), 这是我的观点 //表单字符串@using(Html.BeginForm(“EditProfile”,“Employee”,FormMethod.Post, new {enctype =“multipart / form-data”})) //输入HTML input type =“file”name =
我想创建一个表单,用户可以在其中编辑个人资料并上传图像(如果需要),
这是我的观点

//表单字符串@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函数中的参数仍然是文件为null

仅提及员工模型仅包含图像文件名
因为我只想在数据库中保存图像名称.

解决方法

将HttpFileCollection类型更改为 HttpPostedFileBase.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读