使用Ajax,Json上传文件

时间:2014-09-12 01:18:15

标签: jquery ajax json upload blob

我想使用ajax和json上传文件。 PHP文件有一个包含multipart的表单。我想传递一个文件oo我的js文件,并使用ajax和json将文件传递给我的上传文件。我遇到问题的唯一代码是album_cover,它是一个文件。以下适用于所有输入文本,但不知道如何上传文件。

该文件的ID称为album_cover。

<form action="" method="post" name="artist_create_song_album" id="artist_create_song_album" class="smart-form client-form" enctype="multipart/form-data">
<input type="file" name="album_cover" id="album_cover" placeholder="Album Cover">

调用以下JS文件。

runAllForms();
$(function () {
    $("#artist_create_song_album").validate({
        rules: {
            album_cover: {
                required: true
            },
            song_album_name: {
                required: true
            },
            song_album_description: {
                required: true
            }
        },
        // messages for form validation
        messages: {
            album_cover: {
                required: 'Please upload album cover'
            },
            song_album_name: {
                required: 'Please enter album name'
            },
            song_album_description: {
                required: 'Please enter album description'
            }
        },

        errorPlacement: function (error, element) {
            error.insertAfter(element.parent());
        },
        submitHandler: function () {
            $('#artist_create_song_album').hide(0);
            $('#art_song_message').hide(0);

            $.ajax({
                url: 'artist_update_songs.php',
                type: 'POST',
                dataType: 'json',
                data: {
                    album_cover: $('#album_cover').val(),
                    song_album_name: $('#song_album_name').val(),
                    song_album_description: $('#song_album_description').val()
                },
                success: function (data) {
                    $('#art_song_message').removeClass().addClass((data.error === true) ? 'error' : 'success')
                    .text(data.msg).show(500);
                    if (data.error === true) {
                        if (data.goto == 1) {
                            window.location = "http://dev.worldmusicstage.com/main.php#/ajax/artist_dashboard_info.php";
                            $('#artist_create_song_album').show(500);
                            delete json;
                        } else {

                            $('#artist_create_song_album').show(500);
                        }
                    }
                    if (data.error === false) {
                        window.location = "http://dev.worldmusicstage.com/main.php#/ajax/artist_dashboard_info.php";
                        $('#artist_create_song_album').show(500);
                        delete json;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $('#art_song_message').removeClass().addClass('error')
                    alert('The error was: ' + errorThrown);
                    alert('The error was: ' + XMLHttpRequest);
                    alert('The error was: ' + textStatus);
                    alert(errorThrown.stack)
                    $('#artist_create_song_album').show(500);
                }
            });
            return false;
        }
    });
});

1 个答案:

答案 0 :(得分:-1)

您无法通过(真相)ajax上传文件。您可以为此iframe创建iframe和目标上传表单(<form target="framename">)。