0

我试图让我网站的用户通过单击附件图像在 Web 浏览器中查看他们刚刚上传的文件。我已经编写了上传功能的代码,现在我只需要一些帮助来查看上传完成后的图像。下面我包含了来自控制器的上传功能和视图附件功能所在的索引视图。如果您需要更多代码来帮助解决此问题,请告诉我,感谢您抽出时间来帮助我!

控制器

[Authorize]
        public String Uploadfile(HttpPostedFileBase file)
        {
            if (file.ContentLength > 0)
            {                
                var fileName = Path.GetRandomFileName();
                var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                file.SaveAs(path);
            }
            return file.FileName;
        }

看法

@model PagedList.IPagedList<DBFirstMVC.Models.bug>

@{
    ViewBag.Title = "BugIndex";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using PagedList;

<h2 class="corporate sifr">@ViewBag.Title</h2>


<div class="crossband">
@using (Html.BeginForm())
{
    <div class="lefty">
        Search Bugs Index: @Html.TextBox("SearchString")
    </div>
        <input type = "submit" value = "Search" class = "button1" />
}

    <div class="righty">
    @Html.ActionLink("Report a Bug", "ReportBugs", "Support", null, new { @class = "button1" })

    </div>

</div>

<div class="crossband">
    <div class="lefty">

    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    of @Model.PageCount
    &nbsp;&nbsp;&nbsp;&nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
    }
    else
    {
        @:<<
        @Html.Raw(" ");
        @:< Prev
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink(">>", "", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, Versions = ViewBag.SelectedVersion })
    }
    else
    {
        @:Next >
        @Html.Raw(" ")
        @:>>
    }
    </div>

    <div class="righty">
    Showing Records @Model.FirstItemOnPage to @Model.LastItemOnPage from @Model.TotalItemCount records
    </div>
</div>


<table>
    <tr>

        <th>
            @Html.ActionLink("Date Submitted", "BugIndex", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "date_submitted"})
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "date_submitted"){<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "date_submitted"){<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>
        <th>
            @Html.ActionLink("Submitted By", "BugIndex", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "submitted_by"})
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "submitted_by"){<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "submitted_by"){<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>
        <th>
            @Html.ActionLink("Description", "BugIndex", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "Description" })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "Description"){<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "Description"){<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>
        <th>
            @Html.ActionLink("Priority", "BugIndex", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "Priority" })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "Priority"){<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "Priority"){<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>
        <th>
            @Html.ActionLink("Estimated Completion Date", "BugIndex", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "estimated_completion_date" })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "estimated_completion_date"){<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "estimated_completion_date"){<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>
        <th>
            @Html.ActionLink("Status", "BugIndex", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "status" })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "status"){<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "status"){<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>
        <th>
            @Html.ActionLink("Developer Comments", "BugIndex", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "developer_comments" })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "developer_comments"){<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "developer_comments"){<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>
        <th>
            @Html.ActionLink("Attachment", "BugIndex", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "attachment" })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "attachment"){<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "attachment"){<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>

        <th></th>
    </tr>

@{
    var row_class = "odd";
}

@foreach (var item in Model)
{
    row_class = row_class == "odd"? "even" : "odd";    
    <tr class="@row_class">
        <td>
            @Html.DisplayFor(modelItem => item.date_submitted)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.submitted_by)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.priority)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.estimated_completion_date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.status)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.developer_comments)
        </td>
        <td>
            @if (item.attachment != null){<img id="success" src="@Url.Content("~/Images/ico_confirmation_sml.gif")" alt = "attachment" />}
            @if (item.attachment == null) {<img id="failure" src="@Url.Content("~/Images/ico_error_sml.gif")" alt = "no attachment" />}
            <img src = "@Url.Content(string.Format("~/Uploads/{0}", item.attachment))" alt = "download" />                                                                                                                                 
        </td>
        <td>
            @Html.ActionLink("Edit", "EditBugs", new { id = item.bug_pk }) |
            @Html.ActionLink("Delete", "DeleteBugs", new { id = item.bug_pk })
        </td>
    </tr>
}

</table>
4

1 回答 1

1

存储在 App_Data 中的文件将无法访问。为了在网页上显示它们,您必须将它们移动到不同的文件夹,例如 ~/content 或 ~/images。

然后你可以摆脱这样的事情:

<img src="@Url.Content(string.Format("~/images/{0}", Model.RandomFileName))" />
于 2012-07-09T20:55:27.887 回答