ASP. NET MVC File Download sample

Source: Internet
Author: User
Tags visual studio 2010

ylbtech-asp.net MVC:ASP.NET MVC File Download sample

Function Description: ASP. NET MVC File Download sample

2,technologyandtheenvironment ( Technology and environment)

Operating system:

Windows

Development language:

C#

Development framework:

ASP. NET MVC

Database:

Development software:

Microsoft Visual Studio 2010

Development technology

ASP . NET MVC

3,databasedesign ( database design)

4,featurescreenshots ( function)

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>    <Tablestyle= "width:100%">        <thead>            <TD>FileName</TD>            <TD>Size (bytes)</TD>            <TDstyle= "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>                <ahref= ' <%=ResolveUrl ("~/file/download/" + File. Name)%>' ><imgwidth= "+"Height= "+"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 would call list Action to return the file list and display it via File/list VIEWFI Le request to download a certain file, Filecontroller would call the download action to return the stream of the requested File. This source was subject to the Microsoft public license.seehttp://www.microsoft.com/opensource/licenses.mspx#Ms-pl.all other rights reserved.  This CODE and information are 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.\*************************** ************************************************/usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Mvc.Ajax;usingSystem.IO;namespacecsaspnetmvcfiledownload.controllers{ Public classFilecontroller:controller {//Action for list all the files in "~/app_data/download" directory         PublicActionResult List () {//Retrieve the file list.DirectoryInfo dir =NewDirectoryInfo (Server.MapPath ("~/app_data/download/")); //Filter it via LINQ to Object.            varFiles = fromFinchDir. GetFiles ("*.*", Searchoption.topdirectoryonly)whereF.extension! ="EXE"                        SelectF; //Call the corresponding View.            returnView (Files.        ToList ()); }        //Action for returning the binary stream of a specified file.         PublicActionResult Download (stringfn) {            //Check whether the requested file is valid.            stringPFN = Server.MapPath ("~/app_data/download/"+FN); if(!System.IO.File.Exists (PFN)) {                Throw NewArgumentException ("Invalid file name or file not exists!"); }            //Use Binarycontentresult to encapsulate the file content and return it.            return NewBinarycontentresult () {FileName=FN, ContentType="Application/octet-stream", Content=System.IO.File.ReadAllBytes (PFN)}; }    }}
View Code

6,sample| Explain FreeDownload ( example | case Download)

Ylbtech
Source: http://ylbtech.cnblogs.com/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

ASP. NET MVC File Download sample

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.