使用socketio-file-upload上传图片

时间:2017-09-02 02:08:57

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

我尝试使用socketio-file-upload上传文件,到目前为止它完成了相当好的工作,选择"上传图片"按钮正确上传文件到我喜欢的文件夹。我的问题是任何点击"搜索文件"或" ClickHere"上传文件时没有给出您选择的文件的名称。我想要实现的是

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

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);

1 个答案:

答案 0 :(得分:0)

来自Socket.IO File Upload documentation

  

instance.listenOnSubmit(submitButton,input)

     

instance.listenOnInput(input)类似,除了不在输入元素上侦听“change”事件之外,请监听按钮的“click”事件。

     

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';
});