在 ajax 中发送没有输入类型="file" 的文件(音频类型)
Send a file (type audio) without input type="file" in ajax
我在客户端创建一个文件(记录),然后将其发送到我的远程服务器。但是我找不到不使用输入文件的方法,我有文件路径,但是当我需要通过 ajax 发送它时,在 $ _FILES 端 PHP 中没有检测到。如果我创建一个 blob,它可以工作,但文件与记录不匹配。
可能吗?
[更新 1]
该文件是一个audio/mpeg,这个文件是在录音后创建的,我在其中获取位置并可以再次播放。我需要在用户不点击文件输入的情况下进行恢复
HTML
<form enctype="multipart/form-data" id="form_message" method="POST">
<textarea name="message" id="message" value="" placeholder="Ecris quelque chose"></textarea>
<input type="submit" style="display:none;" value="Valider"/>
</form>
JS
fd = new FormData();
fd.append('audiofile', 'filepath.mp3');
// other data
function submit_form_message(fd){
$.ajax({
type: 'POST',
url: "url",
data: fd,
processData: false,
contentType: false,
success: function(data){}
});
}
PHP
if($_FILES['audiofile']['size'] !=0){
if ($_FILES['audiofile']['error'] == 0){
$extensions_valides = array('mp3' , 'wav');
if(in_array($_POST['extension'],$extensions_valides)){
$tmp_name = $_FILES["audiofile"]["tmp_name"];
$name_file = $notId.".".$_POST['extension'];
move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT']."/Bell/sound/".$name_file);
}
}
}
在这里找到这个,我认为这可能是你最好的选择:Using local file for Web Audio API in Javascript
Step 1: create Base 64 sound font
First I need to convert my mp3 to a Base64 encoded string and store it
as JSON. I found a website that does this conversion for me here -
http://www.mobilefish.com/services/base64/base64.php You may need
to remove return characters using a text editor but for anyone that
needs an example I found some piano tones here -
https://raw.github.com/mudcube/MIDI.js/master/soundfont/acoustic_grand_piano-mp3.js
Note that in order to work with my example you're need to remove the
header part data:audio/mpeg;base64,
Step 2: decode sound font to ArrayBuffer
You could implement this yourself but I found an API that does this
perfectly (why re-invent the wheel, right?) -
https://github.com/danguer/blog-examples/blob/master/js/base64-binary.js
Resource taken from - here
Step 3: Adding the rest of the code
Fairly straightforward
var cNote = acoustic_grand_piano.C2;
var byteArray = Base64Binary.decodeArrayBuffer(cNote);
var context = new webkitAudioContext();
context.decodeAudioData(byteArray, function(buffer) {
var source = context.createBufferSource(); // creates a sound source
source.buffer = buffer;
source.connect(context.destination); // connect the source to the context's destination (the speakers)
source.noteOn(0);
}, function(err) { console.log("err(decodeAudioData): "+err); });
由于您传递的是 Base64 内容字符串,因此您发送的不是原始文件,因此不需要 select 文件。然后,您可以在 PHP 中解码 Base64,并将其写入服务器上的新音频文件。
我在客户端创建一个文件(记录),然后将其发送到我的远程服务器。但是我找不到不使用输入文件的方法,我有文件路径,但是当我需要通过 ajax 发送它时,在 $ _FILES 端 PHP 中没有检测到。如果我创建一个 blob,它可以工作,但文件与记录不匹配。
可能吗?
[更新 1]
该文件是一个audio/mpeg,这个文件是在录音后创建的,我在其中获取位置并可以再次播放。我需要在用户不点击文件输入的情况下进行恢复
HTML
<form enctype="multipart/form-data" id="form_message" method="POST">
<textarea name="message" id="message" value="" placeholder="Ecris quelque chose"></textarea>
<input type="submit" style="display:none;" value="Valider"/>
</form>
JS
fd = new FormData();
fd.append('audiofile', 'filepath.mp3');
// other data
function submit_form_message(fd){
$.ajax({
type: 'POST',
url: "url",
data: fd,
processData: false,
contentType: false,
success: function(data){}
});
}
PHP
if($_FILES['audiofile']['size'] !=0){
if ($_FILES['audiofile']['error'] == 0){
$extensions_valides = array('mp3' , 'wav');
if(in_array($_POST['extension'],$extensions_valides)){
$tmp_name = $_FILES["audiofile"]["tmp_name"];
$name_file = $notId.".".$_POST['extension'];
move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT']."/Bell/sound/".$name_file);
}
}
}
在这里找到这个,我认为这可能是你最好的选择:Using local file for Web Audio API in Javascript
Step 1: create Base 64 sound font
First I need to convert my mp3 to a Base64 encoded string and store it as JSON. I found a website that does this conversion for me here - http://www.mobilefish.com/services/base64/base64.php You may need to remove return characters using a text editor but for anyone that needs an example I found some piano tones here - https://raw.github.com/mudcube/MIDI.js/master/soundfont/acoustic_grand_piano-mp3.js Note that in order to work with my example you're need to remove the header part data:audio/mpeg;base64,
Step 2: decode sound font to ArrayBuffer
You could implement this yourself but I found an API that does this perfectly (why re-invent the wheel, right?) - https://github.com/danguer/blog-examples/blob/master/js/base64-binary.js Resource taken from - here
Step 3: Adding the rest of the code Fairly straightforward
var cNote = acoustic_grand_piano.C2; var byteArray = Base64Binary.decodeArrayBuffer(cNote); var context = new webkitAudioContext(); context.decodeAudioData(byteArray, function(buffer) { var source = context.createBufferSource(); // creates a sound source source.buffer = buffer; source.connect(context.destination); // connect the source to the context's destination (the speakers) source.noteOn(0); }, function(err) { console.log("err(decodeAudioData): "+err); });
由于您传递的是 Base64 内容字符串,因此您发送的不是原始文件,因此不需要 select 文件。然后,您可以在 PHP 中解码 Base64,并将其写入服务器上的新音频文件。