ASP. NET MVC file upload and file download

Source: Internet
Author: User
Tags urlencode

1. Preface

This feature point is needed in recent projects, but it is always garbled when downloaded online. So take advantage of this time to sort out their own, and later need to look directly at their own blog on the line. Has been tested: Google, Firefox, IE and other browsers will not appear garbled problem.

2. Results display

2.1. Upload file Success Interface

2.2. Download the file successful interface

3. Upload file code

3.1. View Code

@model system.web.httpcontextbase@{viewbag.title="Uploading Files";} @*new {enctype = "multipart/form-data" } is less than the upload file will not be successful * @@using (html.beginform ("Upload","UploadFile", FormMethod.Post,New{enctype ="Multipart/form-data" })){    <text> Select Upload file: </text><input name="file"Type="file"Id="file"/> <br/> <br/> <input type="Submit"Name="Upload"Value="Upload"/>}

3.2. Controller code

[HttpPost] Publicactionresult Upload (formcollection form) {if(Request.Files.Count = =0){                //Request.Files.Count files are 0 failed to upload                returnView (); }            varFile = request.files[0]; if(file. ContentLength = =0){                //file size is large (in bytes) is 0 o'clock, do some work                returnView (); }            Else{                //The file size is not 0File = request.files[0]; //save to your own file full path, NewFile is the file you saved after uploading,//the UploadFile folder on the server must have read and write permissions                stringtarget = Server.MapPath ("/")+("/mock/learning/");//get the path to the destination folder                stringfilename = file. FileName;//get the file name                stringPath = target + filename;//gets the destination address of the storefile.            SaveAs (path); returnView (); }

4. Download the file code

4.1. View Code

<a href="/downloadfile/[email protected]&filename= ' little Prince. pdf '"> Download </a >

4.2. Controller Code

 PublicActionResult Download (stringFilePath,stringfileName)            {Encoding Encoding; stringOutputFileName =NULL; FileName= Filename.replace ("'",""); stringBrowser =Request.UserAgent.ToUpper (); if(Browser. Contains ("MS") ==true&& Browser. Contains ("IE") ==true) {OutputFileName=Httputility.urlencode (fileName); Encoding=Encoding.default; }            Else if(Browser. Contains ("FIREFOX") ==true) {OutputFileName=FileName; Encoding=encoding.getencoding ("GB2312"); }            Else{outputfilename=Httputility.urlencode (fileName); Encoding=Encoding.default; } FileStream FS=NewFileStream (FilePath, FileMode.Open); byte[] bytes =New byte[(int) fs.            Length]; Fs. Read (Bytes,0, Bytes.            Length); Fs.            Close (); Response.Charset="UTF-8"; Response.ContentType="Application/octet-stream"; Response.ContentEncoding=encoding; Response.AddHeader ("content-disposition","attachment; Filename="+outputfilename);            Response.BinaryWrite (bytes);            Response.Flush ();            Response.End (); return NewEmptyresult (); }

ASP. NET MVC file upload and file download

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.