socket.io如何上傳圖片并發送到服務器?

我有一個允許用戶上傳圖像的輸入,我如何才能把這個圖像發送到服務器上呢?

到目前為止,我的情況是:

server.js

const express = require("express");
const path = require("path");
const app = express();
const http = require("http").Server(app);
const io = require("socket.io")(http);

var PORT = 7778;
app.use(express.static(path.join(__dirname)));
app.get("/", (req, res) => {
    res.sendFile(path.join(__dirname, "index.html"));
});
io.on("connection", (socket) => {
    socket.on("send-img", () => {
        console.log("img recieved");
    });
});
http.listen(PORT, () => {
    console.log("Server started on port: " + PORT);
});

client.html

<html>
    <head>
        <script src="/socket.io/socket.io.js"></script>
    </head>
    <body>
        <h1>Works!</h1>
        <input type="file" accept=".jpg, jpeg, png" multiple id="img-uplaod" />
        <button id="img-submit">Click Me</button>
    </body>
    <script>
        const socket = io();
        document.getElementById("img-submit").onclick = function () {
            socket.emit("send-img");
        };
    </script>
</html>

下面的答案幾乎可以,但我得到了這個錯誤:

   at Object.openSync (fs.js:443:3)
    at Object.writeFileSync (fs.js:1194:35)
    at Socket.socket.on (/home/pyroot/Desktop/main-dev/projects-main/sell-anything/server.js:19:6)
    at Socket.emit (events.js:198:13)
    at /home/pyroot/Desktop/main-dev/projects-main/sell-anything/node_modules/socket.io/lib/socket.js:528:12
    at process._tickCallback (internal/process/next_tick.js:61:11)


? 最佳回答:

你要做的第一件事就是序列化圖像。這意味著我們需要將圖像文件轉換成可以通過互聯網傳輸的內容,在這種情況下,最好的選擇是base64編碼。以下是您的解決方案:

Client-side

<script>
    const socket = io();
    document.getElementById("img-submit").onclick = function () {
        const ourFile = document.getElementById('img-uplaod').files[0];
        const reader = new FileReader();
        reader.onloadend = function() {
             socket.emit("send-img", reader.result);
        }
        reader.readAsDataURL(ourFile);
    };
</script>

Server-side

const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();
const http = require("http").Server(app);
const io = require("socket.io")(http);

var PORT = 7778;
app.use(express.static(path.join(__dirname)));
app.get("/", (req, res) => {
    res.sendFile(path.join(__dirname, "index.html"));
});
io.on("connection", (socket) => {
    socket.on("send-img", (image) => {
        const splitted = image.split(';base64,');
        const format = splitted[0].split('/')[1];
        fs.writeFileSync('./image.' + format, splitted[1], { encoding: 'base64' });
    });
});
http.listen(PORT, () => {
    console.log("Server started on port: " + PORT);
});
主站蜘蛛池模板: 久久精品日韩一区国产二区| 国产精品av一区二区三区不卡蜜 | 色婷婷亚洲一区二区三区| 欧美激情一区二区三区成人| 国偷自产av一区二区三区| 国产乱码精品一区二区三区香蕉| 中文字幕精品一区二区| 农村人乱弄一区二区| 国产激情一区二区三区四区| 亚洲中文字幕在线无码一区二区| av无码免费一区二区三区| 国模少妇一区二区三区| 大屁股熟女一区二区三区| 一区二区三区免费视频播放器 | 精品无码国产一区二区三区AV| 亚洲成AV人片一区二区| 中日韩一区二区三区| 久久久精品人妻一区二区三区蜜桃 | 国产日韩AV免费无码一区二区| 国产精品毛片一区二区三区| 亚洲AⅤ视频一区二区三区| 国产一区二区免费在线| 国产综合一区二区| 国产精品揄拍一区二区| 日本一区二区在线免费观看| 精品视频一区二区三区免费| 久久精品一区二区三区中文字幕| 天堂国产一区二区三区| 在线精品日韩一区二区三区| 爆乳无码AV一区二区三区| 无码中文人妻在线一区二区三区| 日本一区二区三区中文字幕| 日本一区二区免费看| 久久精品道一区二区三区| 少妇激情一区二区三区视频| 夜夜高潮夜夜爽夜夜爱爱一区| 亚洲熟妇av一区二区三区漫画| 久久久久人妻精品一区三寸| 奇米精品一区二区三区在| 高清国产AV一区二区三区| 国产无吗一区二区三区在线欢|