mvc file upload and download

upload

<form action="/Home/UploadFile" method="post" enctype="multipart/form-data">
    <input type="file" name="file" /><br />
    <input type="submit" value="提交" />
</form>

  Backstage

[HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
            var files = Request.Files; //Method to get files
            if (files != null && files.Count >1)
            {
                #region Perform multiple file uploads
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFileBase fileitem = files[i];
                    // Determine the size of the file
                    string strExtension = Path.GetExtension(fileitem.FileName);
                    double dFileSize = fileitem.ContentLength;
                    if (dFileSize > 5242880)//1024*1024*5)
                    {
                        return Content("<script>alert('" + fileitem.FileName + "文件大于5MB')</script>");
                    }
                    else
                    {
                        //Create a file
                        string filePath = "~/Files";
                        Directory.CreateDirectory(Server.MapPath(filePath));
                        //create unique filename
                        string fileName = Guid.NewGuid().ToString();
                        string fFullDir = filePath + fileName + strExtension;
                        fileitem.SaveAs(Server.MapPath(fFullDir));
                    }
                }
                #endregion
            }
            else
            {
                #region Perform a single file upload
                if (file != null)
                {
                    //You can determine its size format
                    //create folder
                    string filePath = "~/Files";              
                    string path = Server.MapPath(filePath);
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    int size = file.ContentLength;
                    string timePath = DateTime.Now.ToString("yyyyMMddHHmmss");
                    string fileName = timePath+file.FileName;
                    string  pathh = Path.Combine(path, fileName);
                    file.SaveAs(pathh);
                    //return Content("<script>alert('Upload successful!');location.href="
                    //    + Url.Content(filePath) + "</script>");
                }
                #endregion
            }
            return View();
        }

  download:

   <li>@Html.ActionLink("文件下载", "FileDown", "Home")</li>

  public FileStreamResult FileDown()
        {
            string fileName = "aaa.txt";//The file name saved by the client
            string filePath = Server.MapPath("~/Files/Common methods.txt");//Path "D:\\abc.xls";//File path
            return File(new FileStream(filePath, FileMode.Open), "text/plain",
            fileName);
        }

  online method:

method one:
public FileStreamResult DownFile(string filePath, string fileName)
{
      string absoluFilePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] +      filePath);
       return File(new FileStream(absoluFilePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));
}




Method two:
public ActionResult DownFile(string filePath, string fileName)
{
filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] + filePath);
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/octet-stream";


Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
return new EmptyResult();


}

View call:


<a href="/Document/[email protected]&[email protected]">下载</a>

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325740641&siteId=291194637