将base64编码的字符串存储到指定文件夹
Store base64-encoded string to specified folder
美好的一天!我只想将捕获的图像放在指定的文件夹中,而不是通过 ajax 调用放在数据库本身中。这就是我所做的:
JS 文件:
$('#btn-save-snapshot').on('click', function() {
Webcam.snap(function(data_uri)
{
$.ajax({
url: '/capture',
type: 'POST',
data:
{
captimage: data_uri
},
success: function(data)
{
alert('Saved!');
},
error: function(xhr)
{
alert($.parseJSON(xhr.responseText)['error']['message']);
}
});
});
});
不知道如何保存到指定文件夹。我已经在控制器上使用了 Input::file('captimage')->move($destination) 但它不起作用。
你只会得到一个字符串,而不是图像本身(通常是 base64
格式)。因此,您要做的是将信息发送到单独的 php 文件,然后使用 file_put_contents
保存图像。在保存之前,你必须删除前缀。
所以你可以做类似的事情:(如果你得到一个 base64
-string):
file_put_contents('/capture/img.jpg', base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $_POST['captImage'])));
如果不是base64,请post结果,所以你的captImage
变量的内容。
美好的一天!我只想将捕获的图像放在指定的文件夹中,而不是通过 ajax 调用放在数据库本身中。这就是我所做的:
JS 文件:
$('#btn-save-snapshot').on('click', function() { Webcam.snap(function(data_uri) { $.ajax({ url: '/capture', type: 'POST', data: { captimage: data_uri }, success: function(data) { alert('Saved!'); }, error: function(xhr) { alert($.parseJSON(xhr.responseText)['error']['message']); } }); }); });
不知道如何保存到指定文件夹。我已经在控制器上使用了 Input::file('captimage')->move($destination) 但它不起作用。
你只会得到一个字符串,而不是图像本身(通常是 base64
格式)。因此,您要做的是将信息发送到单独的 php 文件,然后使用 file_put_contents
保存图像。在保存之前,你必须删除前缀。
所以你可以做类似的事情:(如果你得到一个 base64
-string):
file_put_contents('/capture/img.jpg', base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $_POST['captImage'])));
如果不是base64,请post结果,所以你的captImage
变量的内容。