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
  • 相关阅读:
    list浅析
    C#尝试读取或写入受保护的内存。这通常指示其他内存已损坏(catch不起作用)
    浅析C#线程同步事件-WaitHandle
    C#操作xml方法1
    C#简单的操作csv文件
    C#的int类型?,??,~的意思,string类型空值赋值
    将多个exc表格汇总于一个表格中
    C#禁止双击标题栏等操作
    c#泛型
    c#session
  • 原文地址:https://www.cnblogs.com/cf425/p/13361587.html
Copyright © 2011-2022 走看看