Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.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
Asp.net mvc MVC 3-图像上载失败_Asp.net Mvc - Fatal编程技术网

Asp.net mvc MVC 3-图像上载失败

Asp.net mvc MVC 3-图像上载失败,asp.net-mvc,Asp.net Mvc,我正在尝试编写一个程序,允许用户向数据库添加图像,但失败了。如果有人能帮助我,我将不胜感激。。。 我的Index.cshtml文件包含以下代码 <td> @item.Picture1 <img alt="@Html.Encode(item.Picture1)" src='@(Url.Action(("Picture1")) + ToString())' width="64" height="64" /> </td> [HttpPost

我正在尝试编写一个程序,允许用户向数据库添加图像,但失败了。如果有人能帮助我,我将不胜感激。。。 我的Index.cshtml文件包含以下代码

 <td>
   @item.Picture1
   <img alt="@Html.Encode(item.Picture1)" src='@(Url.Action(("Picture1")) + 
    ToString())' width="64" height="64" />
</td>
[HttpPost]
    public ActionResult Create([Bind(Exclude = "SubProductCategoryID")] SubProductCategory2 Createsubcat2, FormCollection values)
    {


        if (ModelState.IsValid)
        {
            if (Request.Files.Count > 0)
            {
             Createsubcat2.Picture1 = (new FileHandler()).uploadedFileToByteArray((HttpPostedFileBase)Request.Files[0]);
            }
            db.AddToSubProductCategory2(Createsubcat2);
            db.SaveChanges();
            return RedirectToAction("/");
        }
        PopulateProductCategoryDropDownList(Createsubcat2.ProductCategoryID);
        return View(Createsubcat2);
    }

    public FileResult Image(int id)
    {
        const string alternativePicturePath = @"/Content/question_mark.jpg";
        SubProductCategory2 product = db.SubProductCategory2.Where(k => k.SubProductCategoryID == id).FirstOrDefault();

        MemoryStream stream;

        if (product != null && product.Picture1 != null)
        {
            stream = new MemoryStream(product.Picture1);
        }
        else // If product cannot be found or product doesn't have a picture
        {
            stream = new MemoryStream();

            var path = Server.MapPath(alternativePicturePath);
            var image = new System.Drawing.Bitmap(path);

            image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Seek(0, SeekOrigin.Begin);
        }

        return new FileStreamResult(stream, "image/jpeg");
    }
FileHandler.cs

public class FileHandler
{
    /// <summary>
    /// Converts a HttpPostedFileBase into a byte array
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    public byte[] uploadedFileToByteArray(HttpPostedFileBase file)
    {
        int nFileLen = file.ContentLength;
        byte[] result = new byte[nFileLen];

        file.InputStream.Read(result, 0, nFileLen);

        return result;
    }    
公共类文件处理程序
{
/// 
///将HttpPostedFileBase转换为字节数组
/// 
/// 
/// 
公共字节[]上载的FileToByteArray(HttpPostedFileBase文件)
{
int nFileLen=file.ContentLength;
字节[]结果=新字节[nfilen];
file.InputStream.Read(结果,0,nFileLen);
返回结果;
}    
提前感谢。我正在使用网站作为参考。我正在使用C#SQL Server 2008R2中的VS 2010、ASP.NET 4.0、MVC 3。SubProductCategory2表具有文件Picture1、图像数据类型

编辑:
实际上,我在其中一个类中有一个公共字节[]Picture1{get;set;}。我现在得到的输出是System.byte[]System.byte[]。如何解决这个问题


除了具有文件类型的输入外,还需要确保表单接受多部分数据

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


我无法查看您的表单元素以确认您是否执行了此操作。

什么失败?您有代码,但没有描述您遇到的错误/问题。失败原因是什么?您是否收到错误?运行应用程序时没有错误。当我浏览以查找图片,然后单击“创建”时,页面将返回到索引,其中no图片仅显示用户输入的文本。您好!我需要@using(Html.BeginForm())中实际处理更多内容的代码在哪里?我在其中一个类中有公共字节[]图片1{get;set;}。我现在得到的输出是System.byte[]System.byte[]。如何修复此问题?因为我收到了错误System.byte[]这个帖子有一个不同的问题。而且没有人回应。我会问一个新问题。所以我希望国防部不会介意。谢谢
<form method="POST" enctype="multipart/form-data" action="">