无法在 mvc 中使用 fileupload 更新图像

发布于 2024-11-13 07:57:35 字数 3886 浏览 3 评论 0原文

我正在尝试更新已更新到数据库的文本和图像。我有一个管理部分,其中有一个新闻菜单,用户可以在其中编辑和更新带有图像的新闻。问题是我可以编辑和更新新闻文本,但图像没有更新。下面是控制器和视图:

 [HttpPost]
    [ValidateInput(false)]
    public ActionResult Edit(int id, FormCollection collection, IEnumerable<HttpPostedFileBase> files)
    {
        INewsRepository newsResp = new NewsRepository();
        News news = newsResp.GetNews(id);

        if (TryUpdateModel(news)){
            newsResp.Save();
            return RedirectToAction("Index");
        }else{
            return View();
        }
    }
<% using (Html.BeginForm("Edit", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
                  { %>

                <%: Html.ValidationSummary(true) %>
                <table cellpadding="2" cellspacing="2" border="0">
                    <tr>
                        <td style="width:100px;">
                            <div class="editor-label">
                                Title</div>
                        </td>
                        <td>
                            <div class="editor-field">
                                <%: Html.TextBoxFor(model => model.Title, new { style = "width:300px;" })%>
                                <%: Html.ValidationMessageFor(model => model.Title)%>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div class="editor-label">
                                Article content</div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div class="editor-field">
                                <%: Html.TextAreaFor(model => model.Article, new { @class = "tinymce" })%>
                                <%: Html.ValidationMessageFor(model => model.Article)%>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Image 1
                        </td>
                        <td>

                            <img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge %>" />
                            <br />

                            <input type="file" name="files" id="file1" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Image 2
                        </td>
                        <td>

                            <img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge2 %>" />
                            <br />

                            <input type="file" name="files" id="file2" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Image 3
                        </td>
                        <td>
                            <img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge3 %>" />
                            <br />

                            <input type="file" name="files" id="file3" />
                        </td>
                    </tr>
                </table>

                <p>
                    <input type="submit" value="Save" />
                </p>
                <% } %>

I am trying to update the text and images which is already updated to the databases. I have got a admin section in which there is a news menu, where the user can edit and update the news with images. The problem is i can edit and update the news text but the images doesnt update . Below is the controller and view :

 [HttpPost]
    [ValidateInput(false)]
    public ActionResult Edit(int id, FormCollection collection, IEnumerable<HttpPostedFileBase> files)
    {
        INewsRepository newsResp = new NewsRepository();
        News news = newsResp.GetNews(id);

        if (TryUpdateModel(news)){
            newsResp.Save();
            return RedirectToAction("Index");
        }else{
            return View();
        }
    }
<% using (Html.BeginForm("Edit", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
                  { %>

                <%: Html.ValidationSummary(true) %>
                <table cellpadding="2" cellspacing="2" border="0">
                    <tr>
                        <td style="width:100px;">
                            <div class="editor-label">
                                Title</div>
                        </td>
                        <td>
                            <div class="editor-field">
                                <%: Html.TextBoxFor(model => model.Title, new { style = "width:300px;" })%>
                                <%: Html.ValidationMessageFor(model => model.Title)%>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div class="editor-label">
                                Article content</div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div class="editor-field">
                                <%: Html.TextAreaFor(model => model.Article, new { @class = "tinymce" })%>
                                <%: Html.ValidationMessageFor(model => model.Article)%>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Image 1
                        </td>
                        <td>

                            <img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge %>" />
                            <br />

                            <input type="file" name="files" id="file1" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Image 2
                        </td>
                        <td>

                            <img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge2 %>" />
                            <br />

                            <input type="file" name="files" id="file2" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Image 3
                        </td>
                        <td>
                            <img height="56px" width="75px" alt="image1" src="/content/images/content/<%: Model.ImageLarge3 %>" />
                            <br />

                            <input type="file" name="files" id="file3" />
                        </td>
                    </tr>
                </table>

                <p>
                    <input type="submit" value="Save" />
                </p>
                <% } %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2024-11-20 07:57:35

TryUpdateModel 不会更新文件。您可以手动将流复制到 News 模型的相应属性中:

foreach (var file in files)
{
    byte[] buffer = new byte[file.InputStream.Length];
    file.InputStream.Read(buffer, 0, buffer.Length);
    news.Files.Add(buffer);
}

TryUpdateModel won't update files. You could manually copy the streams into the corresponding property of your News model:

foreach (var file in files)
{
    byte[] buffer = new byte[file.InputStream.Length];
    file.InputStream.Read(buffer, 0, buffer.Length);
    news.Files.Add(buffer);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文