How can javascript upload a blob?

前端 未结 6 2211
旧时难觅i
旧时难觅i 2020-11-22 11:29

I have a blob data in this structure:

Blob {type: \"audio/wav\", size: 655404, slice: function}
size: 655404
type: \"audio/wav\"
__proto__: Blob
6条回答
  •  悲&欢浪女
    2020-11-22 11:59

    I could not get the above example to work with blobs and I wanted to know what exactly is in upload.php. So here you go:

    (tested only in Chrome 28.0.1500.95)

    // javascript function that uploads a blob to upload.php
    function uploadBlob(){
        // create a blob here for testing
        var blob = new Blob(["i am a blob"]);
        //var blob = yourAudioBlobCapturedFromWebAudioAPI;// for example   
        var reader = new FileReader();
        // this function is triggered once a call to readAsDataURL returns
        reader.onload = function(event){
            var fd = new FormData();
            fd.append('fname', 'test.txt');
            fd.append('data', event.target.result);
            $.ajax({
                type: 'POST',
                url: 'upload.php',
                data: fd,
                processData: false,
                contentType: false
            }).done(function(data) {
                // print the output from the upload.php script
                console.log(data);
            });
        };      
        // trigger the read from the reader...
        reader.readAsDataURL(blob);
    
    }
    

    The contents of upload.php:

    
    

提交回复
热议问题