Write in front
The previous article modifies the logic of file upload, which modifies the logic of the file download.
Series Articles
[Ef]vs15+ef6+mysql Code First mode
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (1)
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (2)--User Registration
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (3)--Verification code
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (4)--Upload Avatar
[Bootstrap]modal Popup box
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (5)--login interface, avatar and other proportional compression
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (5)--page template
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (5)--ajax mode registration
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (6)--ajax mode login
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (7)--File upload
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (8)--File download, delete
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (9)--Edit file name
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (10)--New Folder
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (11)--New Folder 2
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (12)--new folder and upload file
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (13)--Edit folder
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (14)--Logical reconstruction
Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (15)--Logical refactoring 2
File download
The downloaded parameters are the same, but this time it is necessary to find the files in the directory Netdisk according to the MD5 of the files. So that you can actually find the physical file.
The code is as follows:
/// <summary> ///File Download/// </summary> /// <param name= "FileId" ></param> Public voidDownLoadFile (stringfileId) {UserInfo UserInfo= session["User"] asUserInfo; if(UserInfo = =NULL) {redirecttoaction ("Login","UserInfo"); return; } if(string. IsNullOrEmpty (fileId)) {Throw NewArgumentNullException ("FileId is Errror"); } intID =Convert.ToInt32 (fileId); varFindFile = _myfileservicerepository.find (x = = X.id = =ID); if(FindFile = =NULL) {alertmsg ("file does not exist",""); return; } stringFilePath = Path.Combine (Server.MapPath ("~/netdisk/"), FINDFILE.FILEMD5 +findfile.fileext); //download a file as a stream of charactersFileStream fs =NewFileStream (FilePath, FileMode.Open); byte[] bytes =New byte[(int) fs. Length]; Fs. Read (Bytes,0, Bytes. Length); Fs. Close (); Response.ContentType="Application/octet-stream"; //notifies the browser to download a file instead of opening itResponse.AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (Findfile.filename, System.Text.Encoding.UTF8)); Response.BinaryWrite (bytes); Response.Flush (); Response.End (); }
Summarize
The logic of the download is also very good to modify, just modify the path of the file, in the database already have MD5 and file extension, this time stitching a file of the true path can be.
[Actual combat]mvc5+ef6+mysql Enterprise Network Disk Combat (15)--Logical refactoring 2