ASP.NET MVC file download sample
2014-12-27 20:40
357 查看
ylbtech- ASP.NET MVC:ASP.NET MVC file download sample |
/****************************** 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) }; } } }
View Code
6,Sample|Explain FreeDownload(示例|讲解案例下载) |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
相关文章推荐
- asp.net mvc动态生成file控件批量上传文件
- [导入]Asp.net Download File 代码
- Asp.Net MVC 使用FileResult导出Excel数据文件
- ASP.NET MVC:通过FileResult向浏览器发送文件
- ProDinner - ASP.NET MVC Sample (EF4.4, N-Tier, jQuery)
- 用ASP.NET MVC自己管理自己的View:ASP.NET MVC File Management
- Asp.net Download File 代码
- 【转载】ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
- Asp.net mvc 3 file uploads using the fileapi
- ASP.NET MVC 4 WebAPI Simple Sample
- File Download Tips for asp.net
- 了解ASP.NET MVC几种ActionResult的本质:FileResult
- 用ASP.NET MVC自己管理自己的View:ASP.NET MVC File Management
- ASP.NET MVC实用FileResult向浏览器发送文件保存详解
- ASP.net MVC FileUpload 文件上传
- ASP.NET MVC实用FileResult向浏览器发送文件保存详解
- How do Download file in the asp.net
- ASP.NET MVC Sample Applications - Open-Source Examples and Tutorials
- asp.net mvc动态生成file控件批量上传文件 .
- [转载]ASP.NET MVC:通过 FileResult 向 浏览器 发送文件