Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (15)--Logical refactoring 2

Source: Internet
Author: User
Tags md5 mysql code

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

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.