Map path to local ASP.NET/MVC file

I have a file upload program written in ASP.NET MVC. It is currently located on my local development machine, and I would like to know how (if possible) to create a link for each downloaded file, so when it is clicked, the item is displayed / loaded, etc.

Current code / markup that handles the display of a list of files:

<table>
    <tr>
        <th></th>
        <th>
            Name
        </th>
        <th>
            Length
        </th>
        <th></th>
    </tr>
    <%
    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploads");
    foreach (var file in Directory.GetFiles(path))
    {
        var item = new FileInfo(file);
    %>
    <tr>
        <td></td>
        <td>
            <%=Html.Encode(Path.GetFileName(item.Name))%>
        </td>
        <td>
            <%=Html.Encode(item.Length >= 1024 ? item.Length / 1024 + " kilobytes" : item.Length + " bytes")%>
        </td>
        <td>
            // This is the line in question. Does not work as-is.
            <a href="<%= item.FullName %>"><%= Html.Encode(Path.GetFileName(item.Name)) %></a>
        </td>
    </tr>
    <% } %>
</table>

I assume that I will have to change the file processing code as soon as this happens, but for now this is enough. Suggestions are also welcome :)

Thanks!

+5
source share
3 answers

Use Url.Content, for example:

<img src="<%= Url.Content("~/Content/UserImages/FileName.jpg") %>" />

" , ". ; , , .

+12

, BaseDirectory ASP.NET - HttpRuntime.AppDomainAppPath. Server.MapPath . HttpContext.Current.Server.

, , , . , , , .

+7
<a href="<%= Url.Content(System.Web.VirtualPathUtility.ToAppRelative("~/" + file.Substring(AppDomain.CurrentDomain.BaseDirectory.Length))) %></a>
+1
source

All Articles