How can I upload a file using jquery's $.ajax function with json and php

后端 未结 8 2322
旧巷少年郎
旧巷少年郎 2020-12-01 23:17

I am trying to upload a file using jQuery\'s $.ajax function but didn\'t get any output. Somebody please help me to solve this. I don\'t know if this script is

8条回答
  •  再見小時候
    2020-12-01 23:45

    This is how I've done it. Use the FormData object.

    Note: The odd syntax of the for statement is just setting "f" to the array[i] instance.

            $("#submit").click(function () {
                var formData = new FormData();
                for (var i = 0, f; f = fileArray[i]; i++) {
                    formData.append("opmlFile", f);
                }
                $.ajax({
                    url: "/Documents/SaveFiles/" + @Model,
                    type: "POST",
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false
                })
                .error(function (xhr, status, error) {
                    $.notify(error, true);
                })
                .success(function (data, status, xhr) {
                    $.notify("Success");
                });
            });
    

    Unfortunately I don't recall which article I got this from, but it was someone else on Stack Overflow.

提交回复
热议问题