4

我正在使用 Angular 7,我想扫描文档并将其保存到数据库表中。

我从下面的链接尝试使用 ngx-document-scanner 和 Dynamic Web TWAIN 但它没有提供将其保存到数据库的选项

https://www.npmjs.com/package/ngx-document-scanner https://blog.dynamsoft.com/imaging/using-dynamic-web-twain-angular-application/

有没有其他方法可以做到这一点?

谢谢

4

1 回答 1

2

Dynamic Web TWAIN 能够通过包括 TWAIN | 在内的协议与物理文档扫描仪进行交互。ICA | Windows 上的 SANE | macOS | Linux。您是否让文档扫描部分在 Angular 应用程序中工作?

扫描页面后,图像将被传输到 Dynamic Web TWAIN 库并存储在内存中或缓存在本地磁盘上。然后,您可以调用一些 API 将数据编码为 5 种格式(bmp、jpg、png、tif、pdf)中的一种,并在 HTTP Post 请求中发送编码数据(文件)。之后,数据离开应用程序,不再与 Angular 相关。提到的 API 将 URL 指定为该 HTTP 请求的接收者。一旦数据到达该 URL,它就会被提取,然后可以保存到数据库中。那么问题来了: 1 您希望将扫描的图像保存成什么格式?2 您使用什么数据库和服务器端语言?

对于一个简单的演示,我将在下面粘贴该 URL 的代码片段,该 URL 是用 PHP for mySQL 编写的

<?php   

    // Interacting with MySQL
    $servername = "127.0.0.1";
    $username = "root";
    $password = "root";
    $dbname = "dwtsample";
    $tablename = "uploadedimages";

    // Create connection
    $conn = new mysqli($servername, $username, $password);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } else {
        // Check Database Existance
        $db_selected = mysqli_select_db($conn, $dbname);
        if(!$db_selected) {
            // Create database
            $sql_newDB = "CREATE DATABASE ".$dbname;
            if ($conn->query($sql_newDB) === TRUE) {
                // echo "Database created successfully";
            } else {
                die("Error creating database: " . $conn->error);
            }           
        }
        mysqli_select_db($conn, $dbname);

        // Check Table Existance
        $sql_showtable = "SHOW TABLES LIKE '".$tablename."'";
        $rowcount = mysqli_num_rows($conn->query($sql_showtable));
        if ($rowcount > 0) {
            // echo "the table exists";
        } else {
            // sql to create table
            $sql_newtable = "CREATE TABLE ".$tablename." (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
            document_name VARCHAR(30) NOT NULL,
            document_data longblob NOT NULL,
            reg_date TIMESTAMP
            )";
            if ($conn->query($sql_newtable) === TRUE) {
                // echo "Table ".$tablename." created successfully";
            } else {
                die("Error creating table: " . $conn->error);
            }
        }           

            $fileTempName = $_FILES['RemoteFile']['tmp_name'];  
            $fileSize = $_FILES['RemoteFile']['size'];
            $fileName = $_FILES['RemoteFile']['name'];
            $strFileSize = (string)intval($fileSize/1024)."KB";
            $fReadHandle = fopen($fileTempName, 'rb');
            $fileContent = fread($fReadHandle, $fileSize);
            fclose($fReadHandle);
            $imgIndex = 0;
            $sql_insertdata = "INSERT INTO ".$tablename." (document_name,document_data) VALUES ('".$fileName."','".addslashes($fileContent)."')";
            if ($conn->query($sql_insertdata) === TRUE) {
                // echo "File saved in db successfully.";
                $sql_getIndex = "SELECT id FROM ".$tablename;
                $IDs = $conn->query($sql_getIndex);
                if ($IDs->num_rows > 0) {
                    // output data of each row
                    while($row = $IDs->fetch_assoc()) {
                        $_temp = intval($row["id"]);
                        if($_temp > $imgIndex)
                            $imgIndex = $_temp;
                    }
                }
            } else {
                die("Error saving file: " . $conn->error);
            }
            $conn->close();
            echo "PHP:"."DWTUploadFileIndex:".strval($imgIndex)."DWTUploadFileName:".$fileName."UploadedFileSize:".$strFileSize;

    }
?>
于 2020-02-07T01:45:44.180 回答