【问题标题】:ajax request for sending file发送文件的ajax请求
【发布时间】:2015-09-22 04:38:20
【问题描述】:

我有一个 jQuery 异常

“数据:formData”行中的“Uncaught TypeError: Illegal invocation”,

当我尝试发送下一个请求时:

$("#loader").on("change", function (data) {                      

            var formData = new FormData($('form')[0]);
            $.ajax({
                url: "/Admin/SaveImage",
                type: "post",
                data: formData,                
                success: function (path) {

                    if (path.value != "") {
                        $("#picture").attr("src", path.value);
                        $("#loader").val(null);
                    }
                }
            });            
        });

【问题讨论】:

  • 它说在哪一行?
  • 什么是path?尝试用 $("#loader").val(""); 替换 $("#loader").val(null); 吗?
  • 谢谢我找到了答案:
  • $("#loader").on("change", function () { if (this.files && this.files[0]) { var reader = new FileReader(); 阅读器。 onload = function (e) { $("#picture").attr("src", e.target.result); } reader.readAsDataURL(this.files[0]); } });

标签: jquery ajax


【解决方案1】:
$("#loader").on("change", function () {

        if (this.files && this.files[0]) {

            var reader = new FileReader();
            reader.onload = function (e) {

                $("#picture").attr("src", e.target.result);
            }
            reader.readAsDataURL(this.files[0]);
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 2011-10-16
    相关资源
    最近更新 更多