c# – HttpPostedFileBase始终返回null,并且不会发布图像

c# – HttpPostedFileBase始终返回null,并且不会发布图像,第1张

概述嗨我发布图片时似乎遇到了问题.我已经在stackoverflow和其他讨论这个主题的论坛上检查了很多问题,但似乎没有提供我需要的答案.这是我的代码: @using( Html.BeginForm("Create", "ProductManager", FormMethod.Post, new{enctype = "multipart/form-data"})){ <ul> 嗨我发布图片时似乎遇到了问题.我已经在stackoverflow和其他讨论这个主题的论坛上检查了很多问题,但似乎没有提供我需要的答案.这是我的代码:

@using( HTML.BeginForm("Create","ProductManager",FormMethod.Post,new{enctype = "multipart/form-data"})){    <ul>        ....        <li>             @HTML.LabelFor(model => model.ProductimagePath,"Avatar")             <input type="file" ID="ProductAvatar" name="ProductAvatar" />             @HTML.HIDdenFor(model => model.ProductimagePath,new { ID = "AvatarHIDdenFIEld"})        </li>         <li>             @HTML.LabelFor(model => model.Productname,"Product name")             @HTML.EditorFor(model => model.Productname)         </li>         .....    </ul>}[httpPost]        public ActionResult Create( FormCollection collection,httpPostedfileBase avatar)        {            string file = collection["ProductAvatar"];            var avatars = avatar;        }

从调试我发现httpPostedfileBase返回null.集合中的其他表单数据成功发布.只有图像没有发布.我似乎无法从FormCollection或httpPostedfileBase访问ProductAvatar,似乎它甚至没有发布

我怎样才能解决这个问题呢?

解决方法 您必须使用将httpPostedfile参数的名称更改为表单上输入文件的相同名称,或者您也可以使用Request.files并通过输入文件的name属性获取,尝试以下 *** 作:

[httpPost]public ActionResult Create(FormCollection collection){   httpPostedfileBase file = Request.files["ProductAvatar"];   if (file.ContentLength > 0)   {      file.SaveAs(/* path */);   }   // othyer tasks   return RedirectToAction("Index");}

name属性是浏览器在提交时在发布/获取表单上发送的内容.

总结

以上是内存溢出为你收集整理的c# – HttpPostedFileBase始终返回null,并且不会发布图像全部内容,希望文章能够帮你解决c# – HttpPostedFileBase始终返回null,并且不会发布图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1222387.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存