在MVC项目中使用C#下载文件?

时间:2015-10-12 18:31:24

标签: c# html asp.net asp.net-mvc download

第一次发帖,如果我犯了任何错误而不遵守指导方针,请更正我。

所以我自己做一个MVC项目。创建一个服务器,将文件分发给注册用户。我有一些HTML经验,所以我尝试使用它来下载文件,但不能。我让浏览器尝试下载文件但是立即说文件无法找到或者不存在。我有一种感觉,因为IIS不允许客户端访问文件系统。当然这只是猜测。我使用Microsoft Visual Studio 2015作为我的IDE,如果它有所作为。这是代码。

@model MainApplication.Models.IndexViewModel
@{
  ViewBag.Title = "Download";
 }

<h2>@ViewBag.Title</h2>

<p class="text-success">@ViewBag.Message</p>

<div>

<ul>
    @{
        string dirPath = "\\Users\\hippiewho\\Documents\\Visual Studio 2015\\Projects\\Web Application\\MainApplication\\Views\\Download";
        string dirDownPath = "..\\Download\\";
        Response.ClearContent();
        Response.Clear();
        System.Web.MimeMapping.GetMimeMapping("w3logo.jpg");
    }
    @{
        List<string> dirs = new List<string>(Directory.EnumerateFiles(dirPath));
        foreach (var dir in dirs)
        {
            <li><a class="btn-block" href="@dirDownPath@dir.Substring(dir.LastIndexOf("\\") + 1)" download >@dir.Substring(dir.LastIndexOf("\\") + 1)</a></li>
        }
    }
    </ul>
</div>

除了v​​iew()函数和下载的ViewBag标题外,控制器没有多少内容。

1 个答案:

答案 0 :(得分:1)

正如梅森所说,你可以从你的动作方法返回一个FileResult。

实施例

val df = sqlContext.read.load("datastore.parquet")    
// the next one would be optimized to use predicate/filter pushdown - and will be rather fast on parquet dataset    
df.filter($"col1"===1).count
/*
== Physical Plan ==
TungstenAggregate(key=[], functions=[(count(1),mode=Final,isDistinct=false)], output=[count#158L])
 TungstenExchange SinglePartition
  TungstenAggregate(key=[], functions=[(count(1),mode=Partial,isDistinct=false)], output=[currentCount#161L])
   Project
    Filter (event_type#0 = clk)
     Scan ParquetRelation[hdfs://...PARQUET][event_type#0]
*/


// the question here - is sample implementation optimized?    
df.sample(false, 0.1).count
/*
== Physical Plan ==
TungstenAggregate(key=[], functions=[(count(1),mode=Final,isDistinct=false)], output=[count#153L])
 TungstenExchange SinglePartition
  TungstenAggregate(key=[], functions=[(count(1),mode=Partial,isDistinct=false)], output=[currentCount#156L])
   Sample 0.0, 0.1, false, -4330576498454505344
    Scan ParquetRelation[hdfs://...PARQUET][]
*/

// combined operation
df.filter($"col1"===1).sample(false, 0.1).count
/*
== Physical Plan ==
TungstenAggregate(key=[], functions=    [(count(1),mode=Final,isDistinct=false)], output=[count#163L])
 TungstenExchange SinglePartition
  TungstenAggregate(key=[], functions=    [(count(1),mode=Partial,isDistinct=false)], output=[currentCount#166L])
   Sample 0.0, 0.1, false, 6556027534253692713
    Project
     Filter (event_type#0 = clk)
      Scan ParquetRelation[hdfs://...PARQUET][event_type#0]
*/

https://msdn.microsoft.com/en-us/library/system.web.mvc.filestreamresult(v=vs.118).aspx