0

我正在尝试上传图像,使用 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();
            }
4

1 回答 1

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();
            }
于 2012-11-27T09:34:21.997 回答