首页 > 解决方案 > 从 blob-url 保存图像(javascript / php)

问题描述

使用 pdf-js 我过滤 PDF 文件的图像。在此之后,我在一个 div 中显示所有图像。元素如下所示:

<img src="blob:http://myhost/07eee62c-8632-4d7f-a086-c06f1c920808">

我想做的是将所有这些图像保存在服务器的目录中。但我不知道该怎么做。

我试过这个,但我认为这是完全错误的:

   let form_data = new FormData();
            fetch(url)
                .then(res => res.blob()) // Gets the response and returns it as a blob
                .then(blob => {
                    // Here's where you get access to the blob
                    // And you can use it for whatever you want
                    // Like calling ref().put(blob)

                    // Here, I use it to make an image appear on the page
                    let objectURL = URL.createObjectURL(blob);
                    let myImage = new Image();
                    myImage.src = objectURL;
                    console.log(id, url, selectedProject, pdfName);
                    form_data.append('file', myImage);
                    form_data.append('path', dest);
                    form_data.append('project', selectedProject);
                    form_data.append('url', url);
                });

            $.ajax({
                url: 'upload_referenceFile.php',
                dataType: 'text',
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,
                type: 'post',
                success: function (php_script_response) {
                }
            });

php:

 $project = $_REQUEST['project'];
$script = $_REQUEST['path'];
$dest = 'data/' .  $project . '/' . $script . '/media/';

_log($_REQUEST['file']);

$exists = file_exists($dest . $_FILES['file']['name']);
if($exists){
}else{
    if (!file_exists($dest)) {
        if (!mkdir($dest, 0777, true) && !is_dir($dest)) {
            throw new \RuntimeException(sprintf('Directory "%s" was not created', $dest));
        }
    }
    move_uploaded_file($_FILES['file']['tmp_name'], $dest . $_FILES['file']['name']);
}

标签: javascriptphpjqueryajaxserver

解决方案


推荐阅读