ASP.NET MVC file download sample
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的更多相关文章
- [转]ASP.NET MVC 2: Model Validation
本文转自:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx?CommentPo ...
- asp.net mvc return file result
asp.net mvc返回文件: public ActionResult ExportReflection(string accessToken) { var reflections = GetCms ...
- 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 ...
- Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码)
1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过开发一款我们自己的APP.本人算是一个基于.net的GIS开发入门者(马上就大四啦), 暑假在学校参加GIS比赛有大把 ...
- 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 ...
- 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 ...
- [转]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 ...
- 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 ...
- [转]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 ...
随机推荐
- Kali 1.0 / 2.0 安装中文输入法(谷歌pinyin + 其他)
1.kali默认是没有中午输入法的,需要自己安装一下 2.首先我们先获取root权限 dnt@HackerKali:~$ su密码: 3.安装中文输入法(apt-get 指令不会的同学可以学习一下基础 ...
- 让VC6.0编译出来的程序支持XP样式或XP风格
(1)VC6.0编译出来的win32程序不支持winxp样式的原因:微软WINXP系统更新了Comctl32.dll(ver 6.0)这个“XP风格”的控件.为了保留传统的Windows界面风格,特地 ...
- bzoj 4569 [Scoi2016]萌萌哒 并查集 + ST表
题目链接 Description 一个长度为\(n\)的大数,用\(S_1S_2S_3...S_n\)表示,其中\(S_i\)表示数的第\(i\)位,\(S_1\)是数的最高位,告诉你一些限制条件,每 ...
- docker push 镜像到本地仓库
root@ubuntu:# uname -a Linux ubuntu --generic #-Ubuntu SMP Mon Feb :: UTC x86_64 x86_64 x86_64 GNU/L ...
- vnc无法显示桌面
转载 以下是我的正确配置,解决上述问题,附带说明: 修改后的~/.vnc/xstartup #!/bin/sh # Uncomment the following two lines for n ...
- Linux 的源码安装工具 CheckInstall
Linux 的源码安装工具 CheckInstall Checkinstall 是一个能从 tar.gz 类的 https://www.ibm.com/developerworks/cn/linux/ ...
- Linux管道符、重定向与环境变量
——<Linux就该这么学>笔记 输入输出重定向输入重定向 指把文件导入到命令中输出重定向 指把原本要输出到屏幕的数据信息写入到指定文件中 输出重定向 分为标准输出重定向和错误输出重定向 ...
- python批量下载淘宝图片3
import urllib.request import os def url_open(url): req = urllib.request.Request(url) req.add_header( ...
- Selenium2+python自动化36-判断元素存在【转载】
前言 最近有很多小伙伴在问如何判断一个元素是否存在,这个方法在selenium里面是没有的,需要自己写咯. 元素不存在的话,操作元素会报错,或者元素有多个,不唯一的时候也会报错.本篇介绍两种判断元素存 ...
- 两个sql设计方案的比较
我有一个买方表Buyer,大概1万条记录:一个卖方表Sale,大概5万条记录.有一些买方和卖方之间是有某种关联的,这种关联关系被记录在Partner表里,Partner表中的关键字段包括BuyerID ...