简体   繁体   English

javascript / PHP文件上传

[英]javascript / PHP file upload

I'm using an audio recorder from this place 我正在从这个地方使用录音机

http://webaudiodemos.appspot.com/AudioRecorder/index.html , http://webaudiodemos.appspot.com/AudioRecorder/index.html

but I instead of saving the file locally I would like to upload it back to the server. 但是我想将其上传回服务器,而不是将文件保存在本地。 My best shot was to try to modify the Recorder.setupDownload function in recording.js script to pass the blob it creates to a simple upload PHP script I found here : 我最好的尝试是尝试修改recording.js脚本中的Recorder.setupDownload函数,以将其创建的blob传递给我在这里找到的简单的上传PHP脚本:

<?php
if(isset($_FILES['image'])){
    $errors= array();
    $file_name = $_FILES['recording']['name'];
    $file_size =$_FILES['recording']['size'];
    $file_tmp =$_FILES['recording']['tmp_name'];
    $file_type=$_FILES['recording']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
    $extensions = array("wav");         
    if(in_array($file_ext,$extensions )=== false){
     $errors[]="extension not allowed, please choose wav file."
    }
    if($file_size > 2097152){
    $errors[]='File size under 20MB';
    }               
    if(empty($errors)==true){
        move_uploaded_file($file_tmp,"images/".$file_name);
        echo "Success";
    }else{
        print_r($errors);
    }
}
?>

And I'm tring it using a jquery call, 我正在使用jquery调用它,

    $.ajax({
            type: "POST",
            url: "../scripts/Single-File-Upload-With-PHP.php",
            data: blob
    });

But I'm obviously doing something wrong. 但是我显然做错了。 The original PHP script has a form in it used for input, which I commented out trying to call the php code directly. 原始的PHP脚本中有一个用于输入的表单,我注释掉了尝试直接调用php代码的过程。

So my questions would be; 所以我的问题是;

  • how to modify the Recorder.setupDownload to upload the file to a designated folder? 如何修改Recorder.setupDownload将文件上传到指定文件夹?
  • how to report back when something goes wrong? 出现问题时如何报告?

Or alternatively, is there a more elegant solution? 或者,是否有更优雅的解决方案?

Edit: Regarding what's in the blob 编辑:关于斑点

This is how the blob is being defined in recorder.js: 这是在recorder.js中定义blob的方式:

worker.onmessage = function(e){
      var blob = e.data;
      currCallback(blob);
}

As to my understanding it is created with methods listed in recorderWorker.js (link in comments), and it should contain simply a wav file. 据我了解,它是使用recorderWorker.js(注释链接)中列出的方法创建的,并且应该只包含一个wav文件。

I dont think you should create the blob in the worker, but I had a similar setup (actually based on the same example) where I retrieved the samplebuffers from the worker and save them into the m_data fields of an AudioMixer class that did some stuff to the recording, then: 我不认为您应该在worker中创建blob,但是我有一个类似的设置(实际上基于同一示例),在其中我从worker中检索了样本缓冲区并将其保存到AudioMixer类的m_data字段中,该类对录音,然后:

//! create a wav file as blob
WTS.AudioMixer.prototype.createAudioBlob = function( compress ){
    // the m_data fields are simple arrays with the sampledata
    var dataview = WTS.AudioMixer.createDataView( this.m_data[ 0 ], this.m_data[ 1 ], this.m_sampleRate );
    return( new Blob( [ dataview ], { type:'audio/wav' } ) );
}

WTS.AudioMixer.createDataView = function( buffer1, buffer2, sampleRate ){
    var interleaved = WTS.AudioMixer.interleave( buffer1, buffer2 );
    // here I create a Wav from the samplebuffers and return a dataview on it
    // the encodeWAV is not provided..
    return( WTS.AudioMixer.encodeWAV( interleaved, false, sampleRate ) );
}

then to send it to the server 然后将其发送到服务器

var blob = this.m_projectView.getAudioEditView().getAudioMixer().createAudioBlob();
if( blob ){
    //! create formdata (as we don't have an input in a DOM form)
    var fd = new FormData();
    fd.append( 'data', blob );

    //! and post the whole thing @TODO open progress bar
    $.ajax({
        type: 'POST',
        url: WTS.getBaseURI() + 'mixMovie',
        data: fd,
        processData: false,
        contentType: false
    } );
}

and I had a node server running where the blob was sent to and could be picked up directly as a wav file, using the express node module: 我有一个节点服务器运行,将blob发送到该节点服务器,并且可以使用express节点模块将其直接作为wav文件提取:

var express = require( 'express' );

// we use express as app framework
var app = express();

/** mixMovie expects a post with the following parameters:
  * @param 'data' the wav file to mux together with the movie 
  */
app.post( '/mixMovie', function( request, response ){

    var audioFile = request.files.data.path;
    ....

} );

hope this helps.. 希望这可以帮助..

Jonathan 乔纳森

In the end this worked nicely for me: 最后,这对我很好:

recorder.js: recorder.js:

$.ajax(
      {
       url: "./scripts/upload.php?id=" + Math.random(),
       type: "POST",
       data: fd,
       processData: false,
       contentType: false,
       success: function(data){
                    alert('Your message has been saved. \n Thank you :-)');
                } 
       });

And the upload script itself: 以及上传脚本本身:

<?php
if(isset($_FILES['data']))
{   
    echo $_FILES['data']["size"];
    echo $_FILES['data']["type"];
    echo $_FILES['data']["tmp_name"];

    $name = date(YmdHis) . '.wav';

    move_uploaded_file($_FILES['data']["tmp_name"],"../DEST_FOLDER/" . $name);

}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM