C # implement file upload and file download,

Source: Internet
Author: User

C # implement file upload and file download,

Public ActionResult Upload () {// var pathUrl = "http: //" + Request. url. authority; var file = Request. files ["Filedata"]; var uploadFileName = file. fileName; string filePath = "/File/" + uploadFileName; string AbsolutePath = Server. mapPath (filePath); file. saveAs (AbsolutePath); // Save the uploaded items in return Json (new {FileName = uploadFileName, FilePath = filePath });}
Public ActionResult DownLoad (string FileName) {string fileName = FileName; // the file name saved by the client string filePath = Server. mapPath ("/File/" + FileName); // path // download the File FileStream fs = new FileStream (filePath, FileMode. open); byte [] bytes = new byte [(int) fs. length]; fs. read (bytes, 0, bytes. length); fs. close (); Response. contentType = "application/octet-stream"; // notify the browser to download the file instead of opening Response. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (fileName, System. text. encoding. UTF8); Response. binaryWrite (bytes); Response. flush (); Response. end (); return Json ("");}

 

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.