zoukankan      html  css  js  c++  java
  • Jquery Ajax上传文件 .net mvc接收

    <input id="FileUpload" multiple type="file" onchange="funChange()"/><br />
        <ul id="ulList">
        </ul>
        <input id="btnShow" type="button" onclick="saveFiles()" value="保存" />
    <script type="text/javascript">
            function funChange() {
                var files = $("#FileUpload")[0].files;
                $("#FileUpload").data("jxp", files);
            };
    
            function saveFiles() {
                $("#ulList").empty();
                var fragment = "";
                var formData = new FormData();
                var cacseFiles = $("#FileUpload").data("jxp");
                if (!cacseFiles) {
                    alert("$.cache缓存中不存在上传数据!");
                    return;
                }
                var length = cacseFiles.length;
                for (var i = 0; i < length; i++) {
                    var file = cacseFiles[i];
                    formData.append("FileUpload", file);
                    var fileName = file.name;
                    var fileSize = file.size;
                    var fileType = file.type;
                    fragment += "<li>文件名:" + fileName + "     (文件大小:<b>" + fileSize + "</b> bytes)    文件类型 :" + fileType + "</li>";
                }
                $("#ulList").html(fragment);
                $.ajax({
                    url: '<%=Url.Content("~/Home/GetFiles") %>',
                    type: 'POST',
                    processData: false,
                    contentType: false,
                    data: formData,
                    success: function (data) {
                        if (data.IsSuccessed) {
                            alert(data.msg);
                        }
                    }
                });
            }
        </script>
    public ActionResult GetFiles(List<HttpPostedFileBase> FileUpload)
            {
                if (FileUpload == null || FileUpload.Count <= 0)
                {
                    return Json(new { IsSuccessed = false, msg = "未上传文件!" });
                }
                foreach (var file in FileUpload)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var dic = Server.MapPath("~/Junk/");
                    if (!Directory.Exists(dic))
                    {
                        Directory.CreateDirectory(dic);
                    }
                    if (fileName == null) continue;
                    var path = Path.Combine(dic, fileName);
                    file.SaveAs(path);
                }
                return Json(new { IsSuccessed = true, msg = "上传成功。" });
            }
  • 相关阅读:
    Adobe Photoshop CS6+blend4 获取png图片data数据
    Git使用教程
    wpf-Datagrid每行combobox设置不同值
    wpf-阿里巴巴图库获取path data数据
    wpf-效果
    wpf-datagrid/listbox隔行换色
    wpf-DataGrid分页
    【学习笔记】委托、匿名方法、Lambda表达式和事件
    【学习笔记】泛型
    【学习笔记】VS常用快捷键
  • 原文地址:https://www.cnblogs.com/jxp0202/p/12759746.html
Copyright © 2011-2022 走看看