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 ("");}