zoukankan      html  css  js  c++  java
  • ASP.NET 上传文件方法

    一、ASP.NET MVC  实现上传下载

    1.首先是Form表单提交

       前台:

     <form action='@Url.Action("Upload", "File")' method="post" enctype="multipart/form-data">
          <input type="file" name="file" />
          <input type="submit" value="提交" />
     </form>

    注意form标签已经包括了enctype标签,而method属性则设为”post”,这样设置并不多于因为默认的提交时通过HTTP get方式进行的。

      后台:

     public ActionResult Upload(HttpPostedFileBase file)
                {
                    var fileName = file.FileName;
                    var filePath = Server.MapPath(string.Format("~/{0}", "File"));
                    file.SaveAs(Path.Combine(filePath, fileName));
                    return Content("上传成功");
                }

    2.前台:

    a href='@Url.Action("Download", "File", new { fileName = "323152.jpg" })'>下载文件</a>

      

     
       public FileStreamResult Download(string fileName)
                {
                    string filePath = Server.MapPath(string.Format("~/{0}/{1}", "File", fileName));
                    FileStream fs = new FileStream(filePath, FileMode.Open);
                    return File(fs, "text/plain", fileName);
                }

    3.Ajax实现文件上传

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
        <title></title>
    </head>
    <body>
    <form id="uploadForm" enctype="multipart/form-data">
        文件:<input id="file" type="file" name="file"/>
    </form>
    <button id="upload">上传文件</button>
    </body>
    <script type="text/javascript">
        $(function () {
            $("#upload").click(function () {
                var formData = new FormData($('#uploadForm')[0]);
                $.ajax({
                    type: 'post',
                    url: "http://192.168.1.101:8080/springbootdemo/file/upload",
                    data: formData,
                    cache: false,
                    processData: false,
                    contentType: false,
                }).success(function (data) {
                    alert(data);
                }).error(function () {
                    alert("上传失败");
                });
            });
        });
    </script>
    </html>
    View Code

     二、Web.API 实现上传下载

    /// <summary>
    /// 文件上传
    /// </summary>
    public class UploadController : BaseController
    {    [HttpPost]
        public Ret<object> PostUpload()
        {
            var httpRequest = HttpContext.Current.Request;
            List<string> list = new List<string>();
    
            for (int i = 0; i < httpRequest.Files.Count; i++)
            {
                var file = httpRequest.Files[i];
                if (file.ContentLength < 1 || file == null) continue;
                //文件存盘,返回Url
                list.Add(V9.Helper.Attach.Save(file));
            }
            return new Ret<object>() { error = 0, msg = "上传成功!", data = list };
        }
    }
    View Code
  • 相关阅读:
    【Flume】数据采集引擎Flume
    【Spark】源码分析之SparkContext
    【Spark】源码分析之spark-submit
    【Hadoop故障处理】高可用(HA)环境DataNode问题
    【Hadoop故障处理】全分布下,DataNode进程正常启动,但是网页上不显示,并且DataNode节点为空
    【Hadoop故障处理】在高可用(HA)配置下,8088端口无法访问,resourcemanager进程无法启动问题
    【Spark】算子
    【Java】集合遍历--List和Map的多种遍历方式
    【Java】集合概述Collection、Map
    【Java】abstract,final,static,private,protected,public的区别
  • 原文地址:https://www.cnblogs.com/cf425/p/13361587.html
Copyright © 2011-2022 走看看