2012-07-18 53 views
0

我试图打开或查看用户上传到我的网站的Uploads文件夹中的附件(不保存!)。附件功能将主要用于当前网站上的错误屏幕截图。我有一个错误索引视图,显示用户提交的所有错误,我希望能够通过单击链接来查看附件。我只是不明白如何做这样的事情,我是否需要使用局部视图或其他辅助方法?我试图写一些方法来查看附件,但我不认为我在我看来正确地调用它。我已经包含查看代码和用于上传方法和视图附件方法的控制器代码。如果您需要其他诊断代码,请告诉我。谢谢你的帮助!使用MVC3打开/查看已上传的文件

错误索引视图

@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", "", new { @class = "text" })   
    </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> 

      @ViewBag.Priorities[(item.priority-1)].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/attach.png")" alt = "attachment" />} 
      @Url.Action("", "ViewAttachment", new { id = item.bug_pk}) <!--<----- I think this is the problem--> 

     </td> 
     <td> 
      @Html.ActionLink("Edit", "EditBugs", new { id = item.bug_pk }) | 
      @Html.ActionLink("Delete", "DeleteBugs", new { id = item.bug_pk }) 
     </td> 
    </tr> 
} 

</table> 

控制器方法

[Authorize] 
     public String Uploadfile(HttpPostedFileBase file) 
     { 
      if (file != null && file.ContentLength > 0) 
      { 
       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(Server.MapPath("~/Uploads"), fileName); 
       string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(file.FileName); 
       int iteration = 1; 
       while (System.IO.File.Exists((path))) 
       { 
        fileName = string.Concat(fileNameWithoutExtension, "-", iteration, System.IO.Path.GetExtension(file.FileName)); 
        path = Path.Combine(Server.MapPath("~/Uploads"), fileName); 
        iteration++; 
       } 
       file.SaveAs(path); 
      } 
      return file.FileName; 
     } 

     public ActionResult ViewAttachment(HttpPostedFileBase file) 
     { 

       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(Server.MapPath("~/Uploads"), fileName); 


       return View(file.FileName); 
     } 

回答

0

所以我得到了这个工作,这就是我做到的。

的ViewAttachment控制器改变为允许system.IO方法

public ActionResult ViewAttachment(string fileName) 
     { 
      try 
      { 
       var fs = System.IO.File.OpenRead(Server.MapPath("~/Uploads/" + fileName)); 
       return File(fs, "application/jpg", fileName); 
      } 
      catch 
      { 
       throw new HttpException(404, "Couldn't find " + fileName); 
      } 


     } 

观被修改,以允许将用于链路,而不是字的回形针图像下载

<td> 
     @if (item.attachment != null) 
     { 
      <a href = @Url.Action("ViewAttachment", new { fileName = item.attachment }) > <img src = "@Url.Content("~/Images/attach.png")" alt = "attachment" /> </a> 
     } 
</td> 

的自从我使用现有数据库以来,模型未经修改。

这就是我解决了查看上传到上传文件夹的图像的问题。该解决方案完全适用于MVC 3和MS SQL Server 2008 R2。

3

有一个action方法,它接受一个ID到purtiular图像资源。上传图片后,你将有一个ID /路径图像,显示的是,在网格,这样

@Html.ActionLink("View","ViewAttachment","Bugs",null,new { @id="SomeIdofImage"}) 

的HREF,这将产生一个锚标记的HTML标记像

<a href="Bugs/ViewAttachment/SomeIdofImage">View</a> 

硬编码SomeIdofImage应该与你的动态值(图像识别符)来代替

现在有一个action方法在你Bug控制器读取Id作为参数并返回图像

public ActionResult ViewAttachment(string id) 
{ 
    var imgPath=SomeMethodtoGetTheFullPathToImg(id); 
    return File(imgPath, "image/jpeg") 
} 

假设你有一个方法SomeMethodtoGetTheFullPathToImg它接受的ID和路径返回到存储在服务器中的图像。

+0

感谢您的快速回复,但我不认为我理解您的答案SomeMethodtoGetTheFullPathToImg是什么。我的意思是我知道如果手动检查uploads文件夹,图像已上传,但是需要编写什么样的新方法才能获取完整路径。我的方法使用Path.GetFileName(file.FileName)不是已经做到了吗?我只需要一点澄清。 – Goldentp 2012-07-18 16:13:18

+0

该方法应该返回图像的完整路径(例如:D:\\ SomeFolder \ uploads \ someimage.jpg“) – Shyju 2012-07-18 16:16:31

+0

对不起,我仍然不明白不要这两行获取图像路径var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath(“〜/ Uploads”),fileName);我必须创建一个局部视图才能使其工作,或者我可以使用我已经拥有的bugindex视图?现在,当我单击链接时,它会将我带到http:// localhost:8080/Bugs/ViewAttachment,并且没有与该位置关联的视图,我做错了什么? – Goldentp 2012-07-18 16:30:43