【问题标题】:Upload image with socketio-file-upload使用 socketio-file-upload 上传图片
【发布时间】:2018-02-11 02:02:57
【问题描述】:

我正在尝试使用 socketio-file-upload 上传文件,到目前为止它做得很好,选择“上传图片”按钮将文件正确上传到我喜欢的文件夹。我的问题是,任何单击“搜索文件”或“单击此处”的按钮都会上传文件,而没有给我您选择的文件的名称。我想要实现的是

  • 1.- 按“上传图片”按钮
  • 2.- 显示文件名
  • 3.- 通过单击“单击此处”按钮将文件上传到所选文件夹 这是我正在使用的代码

main.js

//Connection to the socket library
const socket = io();

//This is some classic socket.io event listening
socket.on('data', data => {
    console.log(data);
});
//Here is our socketio-file-upload code
const uploader = new SocketIOFileUpload(socket);

//uploader.listenOnInput(document.getElementById('fileUpload'));
uploader.listenOnInput(document.getElementById('fileUpload'));


uploader.addEventListener('start', (event)=> {
    event.file.meta.extension = 'csv';
});

document.getElementById('file_button').addEventListener('click', uploader.prompt, false);

【问题讨论】:

    标签: node.js sockets file-upload socket.io pug


    【解决方案1】:

    来自Socket.IO File Upload documentation:

    instance.listenOnSubmit(submitButton, input)

    和instance.listenOnInput(input) 一样,除了监听输入元素上的“更改”事件,而是监听按钮的“点击”事件。

    JavaScript:

    instance.listenOnSubmit(document.getElementById("my_button"), document.getElementById("file_input"));
    

    HTML:

    <label>Upload File: <input type="file" id="file_input" /></label>
    <button id="my_button">Upload File</button>
    

    试试这样的:

    main.js

    //Connection to the socket library
    const socket = io();
    
    //This is some classic socket.io event listening
    socket.on('data', data => {
        console.log(data);
    });
    //Here is our socketio-file-upload code
    const uploader = new SocketIOFileUpload(socket);
    
    // When #file_button is clicked, the file in #fileUpload is uploaded.
    uploader.listenOnSubmit(document.getElementById('file_button'), document.getElementById('fileUpload'));
    
    uploader.addEventListener('start', (event)=> {
        event.file.meta.extension = 'csv';
    });
    

    【讨论】:

    • 它似乎插入正确,但在控制台我得到这个: Uncaught TypeError: Can not read property 'files' of null at Object.listenOnSubmit (client.js: 12) at main.js: 12就是这一行:uploader.listenOnSubmit(document.getElementById('file_button'), document.getElementById('fileUpload'));
    • 我不完全确定为什么。看起来问题在于它说#fileUpload 输入不存在,但确实存在。
    猜你喜欢
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2018-02-06
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多