Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何上传;“一”;使用实体框架和MVC4将图像转换为数据库_C#_Asp.net Mvc_Linq_Entity Framework_Razor - Fatal编程技术网

C# 如何上传;“一”;使用实体框架和MVC4将图像转换为数据库

C# 如何上传;“一”;使用实体框架和MVC4将图像转换为数据库,c#,asp.net-mvc,linq,entity-framework,razor,C#,Asp.net Mvc,Linq,Entity Framework,Razor,我正试图在Create操作中为我的PostController编写一个图像上传服务,但是我收到一个编译错误,上面说: “System.Array”不包含“InputStream”/ContentLength和“no”的定义 扩展方法“InputStream”/ContentLength接受类型为的第一个参数 “System.Array” 在这些方面: using (var binaryReader = new BinaryReader(uploadImage.InputStream))

我正试图在
Create
操作中为我的
PostController
编写一个图像上传服务,但是我收到一个编译错误,上面说:

“System.Array”不包含“InputStream”/ContentLength和“no”的定义 扩展方法“InputStream”/ContentLength接受类型为的第一个参数 “System.Array”

在这些方面:

    using (var binaryReader = new BinaryReader(uploadImage.InputStream))
    {
        imageData = binaryReader.ReadBytes(uploadImage.ContentLength);
    }
我只想上传一个文件,没有更多。我也不确定这是否正确:
Html.Begin
表单中的
singlepart/formdata

我确实尝试了
HttpPostedFileBase uploadImage
而不是
HttpPostedFileBase[]uploadImage
,但是我得到了一个错误,即
实例未设置为此行的对象

using (var binaryReader = new BinaryReader(uploadImage.InputStream))
应该注意的是,
post.Picture
在我的post表中是
(varbinary(max))

视图:


您对
单部件/表单数据的担心是正确的,因为它不是有效的编码方法。Html表单支持以下编码:

  • application/x-www-form-urlencoded(默认值)
  • 多部分/表单数据(允许过帐文件)
  • 文本/纯文本
因此,您必须使用
多部分/表单数据
来发布文件

然后可以修改
Create
方法,使其仅接受单个
HttpPostedFileBase


出现此错误是因为您有对象的
数组
,并且您试图使用
ContentLength
/
InputStream
,这是
HttpPostedFileBase

public ActionResult Create([Bind(Include = "BlogUserEmail,CategoryId,Title,ShortDescription,Description")]Post post, HttpPostedFileBase uploadImage, string selectedTags)
而不是
uploadImage.InputStream
如果您尝试只上载一个图像
uploadImage[0],则可以获取第一个元素。InputStream
uploadImage.first().InputStream
。或者您可以删除
[]

此外,您还必须将
enctype
“单部件/表格数据”
更改为
“多部件/表格数据”

输入有名称
上传图像
和操作参数
上传图像,根据需要更改输入

<input type="file" name="uploadImage" class="input-files" />
操作将如下所示。不要忘记更改视图中的
@model

public ActionResult Create(PostCreateModel post)
{
    if (post.uploadImage != null)
    {
        return RedirectToAction("Create");
    }

    byte[] imageData = null;

    using (var binaryReader = new BinaryReader(post.uploadImage.InputStream)) // not set to an instance of an object
    {
        imageData = binaryReader.ReadBytes(post.uploadImage.ContentLength);
    }
    var Image = new Post
    {
         Picture = imageData
    };

    post.Picture = Image.Picture;

通过这些更改,我仍然会收到错误:对象引用未设置为对象的实例。在上载映像[0]上,InputStream行…Picture设置为公共字节[]Picture{get;set;}在我的模型帖子中,按你的方式操作仍然会导致对象引用问题。如果我在另一个模型中操作,或者直接在操作中操作,则没有任何区别。@GarrithGraham看到我的更新,你有
上传图像
,而不是
上传图像
。是的,这修复了对象引用,但是现在我有了另一个问题,没有任何问题在我的数据库中…:(ty tho)寻求帮助
<input type="file" name="uploadImage" class="input-files" />
public class PostCreateModel : Post
{
    public HttpPostedFileBase uploadImage { get; set; }
    public string selectTags { get; set; }
}
public ActionResult Create(PostCreateModel post)
{
    if (post.uploadImage != null)
    {
        return RedirectToAction("Create");
    }

    byte[] imageData = null;

    using (var binaryReader = new BinaryReader(post.uploadImage.InputStream)) // not set to an instance of an object
    {
        imageData = binaryReader.ReadBytes(post.uploadImage.ContentLength);
    }
    var Image = new Post
    {
         Picture = imageData
    };

    post.Picture = Image.Picture;