【问题标题】:Image not upload successfully in mvc 5图片未在 mvc 5 中成功上传
【发布时间】:2017-12-15 00:48:10
【问题描述】:

我正在尝试在 MVC 5 中上传图片,但图片未发布到服务器端,并且

HTML 代码是

下面的代码在表单标签下

<form action="~/Uerprofile/UploadPhotos" method="post" >
<div class="col-md-12 col-sm-12 form-grou">
<div class="col-md-5 col-sm-5 form-group">
   <label class="label lbldesign">Upload Your Photos</label>
                    <input type="file" class="form-control" id="pphoto" name="pphoto" autofocus accept=".png,.jpeg,.jpg" />
                   <br />
                        <input type="submit" class="btn btn-primary"  value="Upload" />

                    </div>

                </div> 

public ActionResult Uploadphotos(HttpPostedFileBase pphoto)
{
        pphoto = Request.Files["pphoto"];

        if (pphoto != null)
        {
            try
            {
                DataModel dm = new DataModel();
                dm.Photo = converttobyte(pphoto);
                TempData["error"] = "Photo Upload Successfully";
            }
            catch (Exception ex)
            {
                TempData["error"] = ex.Message;
                RedirectToAction("EditProfile", "UserProfile");
            }
            return RedirectToAction("EditProfile", "UserProfile");
        }   
}

【问题讨论】:

  • FORM 标签在哪里?
  • 这不可能是你的动作代码,它甚至不会编译。
  • “Uerprofile”是否正确?这看起来像一个错字。您也没有关闭 form 标记。请格外小心,以确保您向我们提供了所有信息,否则我们将无能为力。

标签: javascript c# sql-server html asp.net-mvc-5


【解决方案1】:

如果你想填充Request.Files,你需要确保你的&lt;form&gt;属性为enctype="multipart/form-data"

使用 HTML 助手:

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

如果您没有使用 HTML Helper,请将该属性添加到您的表单标签中。

<form method="POST" enctype="multipart/form-data" action="/SomeUrl">

【讨论】:

  • 但我没有使用 html helper
  • 我知道你是新来的。请花点时间阅读stackoverflow.com/help/someone-answers。您可以将此答案标记为正确,以便其他有相同问题的人知道解决方案并通过投票表示感谢。
【解决方案2】:

试试这个会有帮助

我的查看代码

@using (Html.BeginForm("Index", "ImportData", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    <div class="row">
        <div id="radio" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <div class="input-group col-lg-4 col-md-4 col-sm-12 col-xs-12">
                <input type="file" id="dataFile" name="upload" class="btn btn-info" />
           </div>
        </div>
    </div>
    <br />
    <div class="row">
        <div id="radio" class="col-lg-12 col-md-12 col-sm-12 col-xs-12" align="center">
            <div class="input-group col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <input type="submit" value="Upload" class="btn btn-primary" />
            </div>
        </div>
    </div>
}

我的控制器代码

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase upload)
    {
        if (ModelState.IsValid)
        {

            if (upload != null && upload.ContentLength > 0)
            {


            }
            else
            {
                ModelState.AddModelError("File", "Please Upload Your file");
            }
        }
        return View();
    }

【讨论】:

【解决方案3】:

您的 HTML 没有 &lt;form&gt; 标签,它告诉浏览器如何处理提交按钮的点击。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 2015-10-08
    • 2014-12-04
    • 2021-08-27
    相关资源
    最近更新 更多