ylbtech- ASP.NET MVC:ASP.NET MVC file download sample

  功能描述:ASP.NET MVC file download sample

2,TechnologyAndTheEnvironment(技术与环境)

操作系统:

windows

开发语言:

C#

开发框架:

ASP.NET MVC

数据库:

开发软件:

Microsoft Visual Studio 2010

 开发技术

ASP.NET MVC

3,DatabaseDesign(数据库设计)
4,FeatureScreenshots(功能截图)

4.App_Data

/App_Data/download/testcert2.cer

/App_Data/download/testdata.zip

/App_Data/download/testfile.txt

/App_Data/download/XMLFile2.xml

4.Models

4.Views

/Views/File/List.aspx

  <h2>
All files available for download:</h2>
<table style="width: 100%">
<thead>
<td>
FileName
</td>
<td>
Size(bytes)
</td>
<td style="width: 40">
</td>
</thead>
<%
var fileList = (List<System.IO.FileInfo>)Model;
foreach (var file in fileList)
{
%>
<tr>
<td>
<%= Html.ActionLink(file.Name,"Download",new {Action="Download", fn=file}) %>
</td>
<td>
<%=file.Length %>
</td>
<td>
<a href='<%= ResolveUrl("~/File/Download/"+ file.Name) %>'>
<img width="30" height="30" src='<%= ResolveUrl("~/images/download-icon.gif") %>' />
</a>
</td>
</tr>
<%} %>
</table>

4.Controllers

/Controllers/FileController.cs

/****************************** Module Header ******************************\
Module Name: FileController.cs
Project: CSASPNETMVCFileDownload
Copyright (c) Microsoft Corporation. This module contains the FileController class. FileController is the controller dedicated for file downloading functionality.
For request to list file, FileController will call List Action to return the
file list and display it via File/List view File request to download a certain file, FileController will call the
Download action to return the stream of the requested file. This source is subject to the Microsoft Public License.
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
All other rights reserved. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.IO; namespace CSASPNETMVCFileDownload.Controllers
{
public class FileController : Controller
{
// Action for list all the files in "~/App_Data/download" directory
public ActionResult List()
{
// Retrieve the file list.
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/App_Data/download/")); // Filter it via LINQ to Object.
var files = from f in dir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
where f.Extension != "exe"
select f; // Call the corresponding View.
return View(files.ToList());
} // Action for returning the binary stream of a specified file.
public ActionResult Download(string fn)
{
// Check whether the requested file is valid.
string pfn = Server.MapPath("~/App_Data/download/" + fn);
if (!System.IO.File.Exists(pfn))
{
throw new ArgumentException("Invalid file name or file not exists!");
} // Use BinaryContentResult to encapsulate the file content and return it.
return new BinaryContentResult()
{
FileName = fn,
ContentType = "application/octet-stream",
Content = System.IO.File.ReadAllBytes(pfn)
};
}
}
}
6,Sample|Explain FreeDownload(示例|讲解案例下载)
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

ASP.NET MVC file download sample的更多相关文章

  1. [转]ASP.NET MVC 2: Model Validation

    本文转自:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx?CommentPo ...

  2. asp.net mvc return file result

    asp.net mvc返回文件: public ActionResult ExportReflection(string accessToken) { var reflections = GetCms ...

  3. Asp.net mvc 3 file uploads using the fileapi

    Asp.net mvc 3 file uploads using the fileapi I was recently given the task of adding upload progress ...

  4. Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码)

    1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过开发一款我们自己的APP.本人算是一个基于.net的GIS开发入门者(马上就大四啦), 暑假在学校参加GIS比赛有大把 ...

  5. CRUD Operations In ASP.NET MVC 5 Using ADO.NET

    Background After awesome response of an published by me in the year 2013: Insert, Update, Delete In ...

  6. Professional C# 6 and .NET Core 1.0 - Chapter 41 ASP.NET MVC

    What's In This Chapter? Features of ASP.NET MVC 6 Routing Creating Controllers Creating Views Valida ...

  7. [转]Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-a ...

  8. Areas in ASP.NET MVC 4

    Download source - 2.7 MB Introduction to Areas In this article, we will learn the concept of Areas a ...

  9. [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-fi ...

随机推荐

  1. [bzoj3277==bzoj3473]出现k次子串计数——广义后缀自动机+STL

    Brief Description 给定n个字符串,对于每个字符串,您需要求出在所有字符串中出现次数大于等于k次的子串个数. Algorithm Design 先建立一个广义后缀自动机,什么是广义后缀 ...

  2. nginx启动脚本(class练习)

    说明:使用类的方式编写程序启动脚本(练习) 1 #!/usr/bin/env python import sys import os from subprocess import Popen,PIPE ...

  3. discuz自定义生成单页面

    在pc端,若要生成一个单页面,有一个比较方便的方法是生成新的专题页,然后diy其中的内容. 不过这种做法有两个缺点 1 url太过冗赘 2 只有一个插入url代码功能,没有文本编辑功能 而且文本框小的 ...

  4. javascript三种嵌入方式

    什么是JavaScript? JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户交互的问题,包括使用交互与数据交互,JavaScript是浏览器解释执行的. J ...

  5. python特有的协程

    #转载请联系 什么是协程呢? 线程包含在进程里面,协程包含在线程里面.协程也是和进程.线程一样,可以实现多任务.协程的切换开销比线程更小,不需要保存和恢复线程的状态.最通俗易懂的说法就是,协程是就是一 ...

  6. 最小生成树的Kruskal算法

        库鲁斯卡尔(Kruskal)算法是一种按照连通网中边的权值递增的顺序构造最小生成树的方法.Kruskal算法的基本思想是:假设连通网G=(V,E),令最小生成树的初始状态为只有n个顶点而无边的 ...

  7. 【cocos2d-js官方文档】七、CCFileUtils

    Web引擎 CCFileUtils.js在Web引擎中已经被删除了,原因是FileUtils在原生平台中的文件检索功能在Web端是无法实现的. 二进制文件获取的方法被转移到了cc.loader.loa ...

  8. HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submis ...

  9. 将csv格式的数据文件导入/导出数据库+添加新的字段

    最近一直忙于实验室的事情,没有好好更新博客,在抓包的过程中,遇到了很多问题. 因为我常用Wireshark将抓包信息导出为csv文件,这里简单mark一下将csv文件导入/导出到数据库的2种方法: 一 ...

  10. Cocos2d-Lua 做一个活动转盘

    这类活动你肯定见过 关于转盘类型的活动我相信大家多多少少都接触到了,很多的抽奖界面都是这类型的,今天这篇小文章就简单的总结一下我们游戏中需要实现这样一个效果的时候我们该怎样去做,其实只要是Cocos类 ...