- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有这个 Controller ,我想做的是将图像作为 [byte] 发送到 Controller ,这是我的 Controller :
[HttpPost]
public ActionResult AddEquipment(Product product, HttpPostedFileBase image)
{
if (image != null)
{
product.ImageMimeType = image.ContentType;
product.ImageData = new byte[image.ContentLength];
image.InputStream.Read(product.ImageData, 0, image.ContentLength);
}
_db.Products.Add(product);
_db.SaveChanges();
return View();
}
在我看来:
@using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post)) {
<fieldset>
<legend>Product</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Category)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Category)
@Html.ValidationMessageFor(model => model.Category)
</div>
<div>
<div>IMAGE</div>
<input type="file" name="image" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
但问题是在我的 Controller 上图像的值总是空的,我似乎无法获得关于 HttpPostedFileBase 的任何信息
最佳答案
您需要使用 multipart/form-data 添加 encType
。
@using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post, new {enctype = "multipart/form-data" })) {
如果它是一个 ViewModel,您也可以随时将它添加到您的模型中,如下所示:
public class Product
{
public Product()
{
Files = new List<HttpPostedFileBase>();
}
public List<HttpPostedFileBase> Files { get; set; }
// Rest of model details
}
您可以通过删除不需要的参数来检索文件,即
[HttpPost]
public ActionResult AddEquipment(Product product)
{
var file = model.Files[0];
...
}
关于c# - MVC HttpPostedFileBase 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21998637/
在我的 MVC 代码中,我得到了一个 httppostedfilebase 类型的图像。我的 SQL 数据库中有相应的图像类型列。 我需要知道如何在我的数据库中将此 httppostedfilebas
在我的 MVC 代码中,我得到了一个 httppostedfilebase 类型的图像。我的 SQL 数据库中有相应的图像类型列。 我需要知道如何在我的数据库中将此 httppostedfilebas
我有一个单元测试,我正在模拟 HttpPostedFileBase 文件并将它们放在列表中。单元测试然后像这样调用邮件服务(不相关的参数,如 to、from、subject 被忽略) [Test] p
我正在尝试使用 MVC 实现上传附件功能。我实际执行上传/保存附件的方法需要 HttpPostedFileBase 类型。 public virtual string Upload(HttpPoste
我有 Kendo ThemeBuilder,在 MVC C# 中创建一个主题,因为我有一个模型,其中包含从 themebuilder 返回的 css body 要将它保存到 css 文件,我可以通过创
我在 HttpPostedFileBase 上遇到错误: The type or namespace name 'HttpPostedFileBase'could not be found(are y
我在带有 CSLA 的分布式环境中使用 MVC .NET,我可以从我的网络层之一(例如 Website.MVC)引用 HttpPostedFileBase,但我不能从单独的层(我们称之为 OtherL
在我的Create View中我有 @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl, FormMethod.Post, encty
我正在使用 ASP.NET MVC,并且我有一个上传文件的操作。文件正在正确上传。但我想要图像的宽度和高度。我想我需要先将 HttpPostedFileBase 转换为 Image 然后继续。我该怎么
在我的Create View 我有 @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl, FormMethod.Post, encty
我的客户端javascript代码是 function SendMail() { debugger; var decpr = tinyMCE.get('taskdesc
我正在开发一个 ASP.NET MVC 4 应用程序,在许多 View 中用户可以上传我保存在服务器上的文件。除此之外,我还有一个单独的实体,用于保存上传文件的不同数据,例如: string fi
如果我禁用客户端验证 并尝试上传一个大约 11 MB 的文件,将文件设置为 HttpPostedFileBase memeber [ValidateFile] public Htt
我正在使用 C# 开发一个 ASP.NET MVC 项目,试图将文档从表单上传到我的数据库。我目前在我的 Controller 中,我已将文件导入为 HttpPostedFileBase,并且必须将其
有没有一种简单的方法可以从 HttpPostedFileBase 中获取 FileInfo 对象?我意识到我可以保存文件,然后执行类似 DirectoryInfo.GetFiles 的操作,然后循环遍
那么,场景是这样的:用户上传文件,我的代码将此文件转换为字节数组,然后将该数组传递给外部 API。这很好用。 问题是这个文件包含了像æ,ø,å这样的特殊字符,当byte[]再次转换成字符时,这些字符被
我无法弄清楚我做错了什么,但由于某种原因 HttpPostedFileBase 总是返回 null。当我尝试在服务器端上传文件时,我继续使用 HttpPosterFileBase 获取 null:这是
我正在尝试创建一种方法,我可以在其中传递图像文件并检索该图像文件的字节,以便稍后我可以将字节存储在数据库中。这是我的方法代码。 private byte[] GetImageBytes(HttpPos
我正在尝试在 ASP.NET MVC 中上传单个 .csv 文件。在我的 .ascx 文件中,我有:  
我必须使用具有如下方法的网络服务: SubmitUser(UserReg user, HttpPostedFileBase image) { // webservice side process
我是一名优秀的程序员,十分优秀!