简体   繁体   中英

Need help adding download feature to MVC application

I have a web MVC application that I would like to add feature that gives users the ability to download large files from my server. The users have a combination of Mac and Windows PC. I was thinking along the lines of javasripts or silverlight.

Can someone advice me on how to implement this feature? Do you have any code examples?

Use the File method of Controller class.

So Create a Controller called FilesController and have an action method called DownLoad

public class FilesController : Controller
{  
   public ActionResult Download(string fileId)
   {
     var fullFilePath=FileService.GetFullPath(fileId);  // get the path to file
     return File(fullFilePath,"application/pdf","yourDownLoadName.pdf");  
   }
}

This Will return a PDF file from the specified path( fullFilePath ) with the MimeType/ContentTyp e as PDF and " yourDownLoadName.pdf " as the Downloadable file name

Users can access this like http://yourdomainname.com/Files/Download?fileId=somefileId

This method has got a bunch of overloads using file path, byte array ,stream etc..

使用FileStreamResult返回类型创建控制器操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM