Еще одно: TypeError: Невозможно прочитать путь неопределенного - PullRequest
0 голосов
/ 11 мая 2018

Я также получаю эту знаменитую ошибку при попытке загрузить файл.В моем HTML у меня есть входной файл внутри элемента span .Но если я удаляю это, то моя загрузка файла не работает.И я также хочу, чтобы он отображался только как соответствующий значок .

Может ли это быть проблемой?

<form method="POST" enctype="multipart/form-data" id="frm-chat">
 <div class="file-field input-field">
  <input class="file-path validate" type="text" placeholder="Write your message..." />
   <span class="fa fa-paperclip attachment" aria-hidden="true">
    <input name="file" id="file" type="file">
   </span>
 </div>
 <button class="submit" type="submit">
  <i class="fa fa-paper-plane" aria-hidden="true"></i>
 </button>
</form>

jQuery:

 // Send message with file upload POST 
    $('#frm-chat').submit(function (e) {
        event.preventDefault()
        let file = $('#file').val()
        console.log(file)
        $.ajax({
            url: "/send-message",
            type: "POST",
            data: new FormData(this),
            processData: false,
            contentType: false,
        }).always(function (response) {
            if (response.status == 'error') {
                console.log("error")
                // give some feedback 
                // like open a modal with the error message
            }
            // give some feedback
            console.log("success")
        })

    })

серверный маршрут:

app.post('/send-message', upload.single('file'), (req, res) => {
    console.log(req)
    let sChatFilePath = req.file.path.split("public/").pop()
    /*user.saveMessage( req.body, sChatFilePath, (err, jResult) => {
        if (err) {
            return res.send(jResult)
        }
        return res.send(jResult)
    })*/
    return res.send({
        status: "OK"
    })
})
...