开发者

Cannot update image using fileupload in mvc

开发者 https://www.devze.com 2023-03-10 12:30 出处:网络
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.

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.ValidationMess开发者_JAVA百科ageFor(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>
                <% } %>


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消