Method 1:
/// <Summary>
/// File Download class
/// Call 1: downloadfile ("/2003.xls", null );
/// Call 2: downloadfile ("/2003.xls ","");
/// Call 3: downloadfile ("/2003.xls"," temp.xls ");
/// </Summary>
/// <Param name = "filepath"> file path (Format:/upload/2003.xls) </param>
/// <Param name = "FILENAME"> Custom file name (if you do not want to customize a file name, upload ["" or null]) </param>
Public actionresult downfile (string filepath, string filename)
- {
- Filepath = server. mappath (system. configuration. configurationmanager. deleetpipeline ["attachmentpath"] + filepath );
- Filestream FS = new filestream (filepath, filemode. Open );
- Byte [] bytes = new byte [(INT) fs. Length]; // download an object as a response stream
- FS. Read (bytes, 0, bytes. Length );
- FS. Close ();
- Response. charset = "UTF-8 ";
- Response. contentencoding = system. Text. encoding. getencoding ("UTF-8 ");
- Response. contenttype = "application/octet-stream"; // notify the browser to download the file instead of opening it
- Response. addheader ("content-disposition", "attachment; filename =" + server. urlencode (filename ));
- Response. binarywrite (bytes );
- Response. Flush ();
- Response. End ();
- Return new emptyresult ();
- }
Method 2:
Public filestreamresult downfile (string filepath, string filename)
{
String absolufilepath = server. mappath (system. configuration. configurationmanager. deleettings ["attachmentpath"] + filepath );
Return file (New filestream (absolufilepath, filemode. Open), "application/octet-stream", server. urlencode (filename ));
}
Call method:
<A href = "/document/downfile? Filepath = @ item. Value & filename = @ item. Key "> download </a>