• 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
  • 相关阅读:
    Splay复习
    带权并查集复习-HDU3038
    罗素悖论-图灵停机问题
    数独解法c++实现
    状压DP NOI2001 炮兵阵地
    区间第k大数
    分块随笔T2
    分块感想
    webkit的高级属性
    设计模式
  • 原文地址:https://www.cnblogs.com/cf425/p/13361587.html
Copyright © 2020-2023  润新知