jquery上传文件

2年前 (2022) 程序员胖胖胖虎阿
173 0 0
function initFileUpload(id) {
        var file = $("#" + id + "");
        var files = file[0].files;
        var formData = new FormData();
        for (var i = 0; i < files.length; i++) {
            formData.append(files[i].name, files[i]);
        }
     //formData.append("name", "刘大大");//可追加参数
       $.ajax({
            url: "@Url.Action("UploadImage")",
            type: "POST",
            async: true,
            dataType: "FromData",
            data: formData,
            contentType: false,
            processData: false,
            success: function (data) {
                console.log("data:");
                console.log(data);
            }
        });
  }

后台:

public ActionResult UploadImage()
        {
            var files = HttpContext.Request.Form.Files;
            if (files.Count > 0)
            {
                var fileDirectory = Path.Combine(_hostingEnvironment.ContentRootPath, Config.UploadVirtualPath, "SupplierImage");
            }
            return Json(new { success = true });
        }

 html

<input title="img" type="file" onchange="initFileUpload(\''+data[j].ProcessFlowFieldName+'\')" name='+data[j].ProcessFlowFieldName+' id=\''+data[j].ProcessFlowFieldName+'\'  accept=".png,.jpg,jpeg" onchange="$(\'#location\').val($(\'#'+data[j].ProcessFlowFieldName+'\').val());" style="display: none">

 

版权声明:程序员胖胖胖虎阿 发表于 2022年11月21日 上午11:24。
转载请注明:jquery上传文件 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...