当前位置:编程学习 > asp >>

【译】MVC3 20个秘方-(11)通过表单上传文件

 

 

问题

你希望允许用户在你的网站上传并保存文件。

解决方案

通过HttpPostedFileBase.实现上传文件和保存到磁盘。

讨论

在接下来的例子里,之前创建的去添加和更新图书的View将被更新成允许用户选择一个文件并且上传缩略图文件。作为开始,Book/Create  view 应该被更新,改变From的编码类型并且为缩略图字段替换掉脚手架textbox。代码如下:

@model MvcApplication.Models.Book

@{

    ViewBag.Title = "Create";

}

<h2>

    Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"

type="text/javascript"></script>

<script src="

@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"

type="text/javascript"></script>

@using (Html.BeginForm("Create", "Books", FormMethod.Post,

new { enctype = "multipart/form-data" }))

{

    @Html.ValidationSummary(true)

    <fieldset>

        <legend>Book</legend>

        <div class="editor-label">

            @Html.LabelFor(model => model.Title)

        </div>

        <div class="editor-field">

            @Html.EditorFor(model => model.Title)

            @Html.ValidationMessageFor(model => model.Title)

        </div>

        <div class="editor-label">

            @Html.LabelFor(model => model.Isbn)

        </div>

        <div class="editor-field">

            @Html.EditorFor(model => model.Isbn)

            @Html.ValidationMessageFor(model => model.Isbn)

        </div>

        <div class="editor-label">

            @Html.LabelFor(model => model.Summary)

        </div>

        <div class="editor-field">

            @Html.EditorFor(model => model.Summary)

            @Html.ValidationMessageFor(model => model.Summary)

        </div>

        <div class="editor-label">

            @Html.LabelFor(model => model.Author)

        </div>

        <div class="editor-field">

            @Html.EditorFor(model => model.Author)

            @Html.ValidationMessageFor(model => model.Author)

        </div>

        <div class="editor-label">

            @Html.LabelFor(model => model.Thumbnail)

        </div>

        <div class="editor-field">

            <input type="file" name="file" />

            @Html.ValidationMessageFor(model => model.Thumbnail)

        </div>

        <div class="editor-label">

            @Html.LabelFor(model => model.Price)

        </div>

        <div class="editor-field">

            @Html.EditorFor(model => model.Price)

            @Html.ValidationMessageFor(model => model.Price)

        </div>

        <div class="editor-label">

            @Html.LabelFor(model => model.Published)

        </div>

        <div class="editor-field">

            @Html.EditorFor(model => model.Published)

            @Html.ValidationMessageFor(model => model.Published)

        </div>

        <p>

            <input type="submit" value="Create" />

        </p>

    </fieldset>

}

<div>

    @Html.ActionLink("Back to List", "Index")

</div>

Book/Edit view 也应该以相同的方式被更新,除了添加一个hidden字段(在旧的thumbnail那)。这将用于在BookController中上传新文件之前删除旧的文件。代码如下:

@model MvcApplication.Models.Book

@{

    ViewBag.Title = "Edit";

}

<h2>

    Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"

type="text/javascript"></script>

<script src="

@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"

type="text/javascript"></script>

@using (Html.BeginForm("Edit", "Books", FormMethod.Post,

new { enctype = "multipart/form-data" }))

{

    @Html.ValidationSummary(true)

    <fieldset>

        <legend>Book</legend>

        @Html.HiddenFor(model => model.ID)

        <div class="editor-label">

            @Html.LabelFor(model => model.Title)

        </div>

      &nb

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,