[英]how to create a temp file from a base64 string
我正在嘗試上傳使用javascripts .toDataURL()方法從canvas元素獲取的圖像(base64字符串):
var cropped_base64 = $crop_canvas[0].toDataURL('image/jpeg');
cropped_base64 = cropped_base64.replace('data:image/jpeg;base64,','');
$.ajax({
url:the_url,
data:{
base64_string: cropped_base64
},
type:'POST',
success:function(data){
console.log(data)
},
error:function(data){
console.log(data)
}
});
我的服務器端代碼將其解碼:
my $base64_string = param('base64_string')
or return 'error no base 64';
my $image_decoded = MIME::Base64::decode_base64($base64_string)
or return 'error couldnt decode base64';
我需要將此圖像作為臨時文件傳遞給另一個例程。
我該如何生成呢? 我無法安裝類似Image :: magick的模塊...
您已經獲得了標量的解碼內容。 將標量打印到文件:
open ( my $image, '>:raw', '/path/to/image.png' ) or die $!;
print {$image} $image_decoded;
close ( $image );
如果要避免潛在的競爭條件,則必須對唯一命名進行一些處理。 File::Temp
可能是解決此問題的好方法。
例如:
use File::Temp qw/ tempfile /;
my ( $output_fh, $filename ) = tempfile();
binmode $output_fh;
print {$output_fh} $image_decoded;
這不會自動刪除,因為您要求輸入文件名(然后可以將其傳遞給其他文件)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.