zoukankan      html  css  js  c++  java
  • 基于mvc三层架构和ajax技术实现最简单的文件上传

    前台页面提交文件

    <!DOCTYPE html>

    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>文件操作</title>
    </head>
    <body>
    <form id="upFileForm">
    <div style="margin:30px;">
    <span style="margin-right:10px;">上传文件</span><input type="file" name="UpFile" id="UpFile" style="12%;" />
    <input type="button" onclick="upFile()" value="确认上传" />
    </div>
    </form>

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.form.js"></script>

    <script>

    //上传文件
    function upFile() {
    var file = document.getElementById("UpFile").files[0];
    if (!file) {
    alert("请选择文件!");
    return;
    }
    var size = file.size / 1024 / 1024;
    if (size > 50) {
    alert("图片文件不能大于50M");
    return;
    }
    $("#upFileForm").ajaxSubmit({
    url: "/File/UploadFile",
    type: "post",
    dataType: "json",
    success: function (data) {
    if (data == "" || data == "0") {
    alert("上传失败");
    }
    if (data == "2") {
    alert("不支持上传该文件");
    } else {
    alert(JSON.stringify(data));
    }
    },
    error: function (aa) {
    alert(aa);
    }
    });
    }
    </script>

    </body>
    </html>

    后台接收和上传

    [HttpPost]
    public JsonResult UploadFile()
    {
    HttpRequest request = System.Web.HttpContext.Current.Request;
    HttpFileCollection FileCollect = request.Files;
    string path = "";//文件的完整路径
    //文件保存目录路径
    string imgPathName = DateTime.Now.ToString("yyyyMMdd");//以日期为文件存放的上层文件夹名
    string savePath = "/upload/file/" + imgPathName + "/";//文件存放的完整路径
    //如果文件路径不存在则创建文件夹
    if (!Directory.Exists(Server.MapPath(savePath)))
    {
    Directory.CreateDirectory(Server.MapPath(savePath));
    }
    //定义允许上传的文件扩展名
    Hashtable extTable = new Hashtable();
    extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

    if (FileCollect.Count > 0)//如果集合的数量大于0,多文件上传情况 
    {
    foreach (string str in FileCollect)
    {
    HttpPostedFile imgFile = FileCollect[str];//用key获取单个文件对象HttpPostedFile 
    string fileName = imgFile.FileName;//获取文件名
    string fileExt = Path.GetExtension(fileName).ToLower();//获取文件后缀名
    //判断文件类型是否正确
    if (Array.IndexOf(((string)extTable["file"]).Split(','), fileExt.Substring(1).ToLower()) == -1)
    {
    //文件类型不正确
    return Json("2");
    }
    string imgName = DateTime.Now.ToString("yyyyMMddhhmmss");//文件别名
    string imgPath = savePath + imgName + "-" + imgFile.FileName;//构造文件保存路径 
    string AbsolutePath = Server.MapPath(imgPath);
    imgFile.SaveAs(AbsolutePath);//将上传的东西保存 
    path = imgPath;//获得文件完整路径并返回到前台页面
    }
    return Json(path);
    }
    else
    {
    //上传失败
    return Json("0");
    }
    }

  • 相关阅读:
    《大话数据结构》第1章 数据结构绪论 1.2 你数据结构怎么学的?
    伍迷七八月新浪微博集锦
    《大话数据结构》第9章 排序 9.7 堆排序(下)
    《大话数据结构》第3章 线性表 3.8.2 单链表的删除
    《大话数据结构》第9章 排序 9.5 直接插入排序
    《大话数据结构》第9章 排序 9.8 归并排序(上)
    《大话数据结构》第2章 算法基础 2.9 算法的时间复杂度
    《大话数据结构》第1章 数据结构绪论 1.1 开场白
    《大话数据结构》第9章 排序 9.1 开场白
    [AWS] Assign a public IP address to an EC2 instance after launched
  • 原文地址:https://www.cnblogs.com/wangsir1992/p/9290322.html
Copyright © 2011-2022 走看看