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. Linux提权思路

    先写个大概 0. dirtycow 不同的dirtycow有不同执行条件. 使用前先对照此表根据内核版本确认是否已经修复 https://github.com/dirtycow/dirtycow.gi ...

  2. JSP页面中格式化日期为指顶格式

    有时候在页面中显示直接从数据库获取的日期时候会出现英文的日期格式.比如:

  3. IPsec传输模式下ESP报文的装包和拆包过程

    原创文章,拒绝转载 装包过程 总体流程图 过程描述 在原IP报文中找到TCP报文部分,在其后添加相应的ESP trailer信息. ESP trailer 包含三部分:Padding,Pad leng ...

  4. bootstrapValidator关于verbose需要优化的地方

    开发中需要用到bootstrapValidator的配置verbose:false,达到当前验证不通过不往下在验证的效果 问题: 当前字段需要remote验证时,此配置无效,原因在于remote是异步 ...

  5. 云平台服务运行情况检测脚本V0.1

    1.准备Python3环境 yum groupinstall "Development tools" -y yum install zlib-devel bzip2-devel o ...

  6. 在.NET4.5项目中添加HttpClient引用的办法

    一. 创建新项目 1. 打开  Microsoft Visual Studio 201 0,然后从 “ 文件 ” 菜单中选择 “ 新建项目 ” .在模板列表中,选择  Visual C#.在该区域下面 ...

  7. jquery事件之select选中事件

    根据select下拉列表选中的不同选项执行不同的方法,工作中经常会用到,这里就要用到Jquery的select选中事件 这里给select加一个叫label_id的id,然后通过id选择器找到这个节点 ...

  8. PHP极速开发框架LotusAdmin page版发布

    体验地址及账号如下: 地址    https://page.waytomilky.com/ 账号:admin 密码:123456 LotusAdmin2.0其主要特性包括: 1.基于ThinkPHP5 ...

  9. typescript 定义全局变量以及扩展原生js对象

    使用“declare global”操作即可. 项目根目录下新建myDeclareFile.d.ts declare global { interface Navigator { mediaSessi ...

  10. ZCMU Problem G: 素数对(数论,素数筛法)

    #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #inc ...