图像上载,转换错误InputStream.Read()方法

时间:2012-11-27 08:39:01

标签: asp.net asp.net-mvc

我正在尝试上传图片,使用HttpPostedFileBase.InputStream.Read()方法将上传流读取到缓冲区,但它在

时给出了转换错误

file.InputStream.Read(imageSize,0,file.ContentLength);

请找到下面的代码,我错过了什么

    @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <fieldset>
            <legend>Upload Image</legend>

            @Html.Label("Title")
            @Html.Editor("fileTitle")<br />
            Upload File: <input type="file" name="test" />
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }

    [HttpPost]
            public ActionResult Create(string fileTitle)
            {
                try
                {
                    HttpPostedFileBase file = Request.Files[0];
                    byte[] imageSize = new byte[file.ContentLength];
                    **file.InputStream.Read(imageSize, 0, file.ContentLength);**
                    Image image = new Image()
                    {
                        Name = file.FileName.Split('\\').Last(),
                        Size = file.ContentLength,
                        Title = fileTitle,
                        ID = 1,
                        Image1 = imageSize
                    };
                    db.Images.AddObject(image);
                    db.SaveChanges();
                    return RedirectToAction("Detail");
                }
                catch(Exception e)
                {
                    ModelState.AddModelError("uploadError", e);
                }
                return View();
            }

1 个答案:

答案 0 :(得分:0)

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <fieldset>
            <legend>Upload Image</legend>

            @Html.Label("Title")
            @Html.Editor("fileTitle")<br />
            Upload File: <input type="file" name="test" />
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }

    [HttpPost]
            public ActionResult Create(string fileTitle)
            {
                try
                {
                    HttpPostedFileBase file = Request.Files[0];
                    byte[] imageSize = new byte[file.ContentLength];
                    ***file.InputStream.Read(imageSize, 0, (int)file.ContentLength);***
                    Image image = new Image()
                    {
                        Name = file.FileName.Split('\\').Last(),
                        Size = file.ContentLength,
                        Title = fileTitle,
                        ID = 1,
                        Image1 = imageSize
                    };
                    db.Images.AddObject(image);
                    db.SaveChanges();
                    return RedirectToAction("Detail");
                }
                catch(Exception e)
                {
                    ModelState.AddModelError("uploadError", e);
                }
                return View();
            }