ASP.NET MVC file download sample

來源:互聯網
上載者:User

標籤:

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>
View Code

4.Controllers

/Controllers/FileController.cs

/****************************** Module Header ******************************Module Name:  FileController.csProject:      CSASPNETMVCFileDownloadCopyright (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 viewFile 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 download sample

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.