Anonymous
Node.JS PUG отправляет текст и изображение на серверную часть
Сообщение
Anonymous » 03 ноя 2024, 10:12
У меня есть следующий шаблон PUG:
Код: Выделить всё
extends layout
block append head
link(type='stylesheet', href='/stylesheets/mystyle.css', rel='stylesheet')
block layout-content
#chat-app
#chat.has-text-centered
h1.title Welcome to #{title}
section.section.chat-container
.container
.columns
.box.column.is-full
h2.title Messages
.chat-messages.has-text-left
p#messages
form#chatForm(action='/api/gemini', method='post', enctype="multipart/form-data")
.field.has-addons
p.control.is-expanded
input(class='input', id='prompt', type='text', placeholder='How can I help?')
p.control
input(class='input', id='image', type='file')
p.control
input(class='button is-success', type='submit', value='Post')
script.
document.querySelector("#chatForm").addEventListener("submit", async (e) => {
e.preventDefault();
const prompt = document.querySelector("#prompt").value;
if (prompt.trim()) {
const response = await fetch('/api/myapi', {
method: 'POST',
headers: { 'Content-Type': 'multipart/form-data' },
body: JSON.stringify({, prompt:, prompt})
});
const data = await response.json();
document.querySelector("#messages").innerHTML += `
[b]You:[/b] ${prompt}
`;
document.querySelector("#messages").innerHTML += `
[b]Server:[/b] ${data.message}
`;
document.querySelector("#prompt").value = '';
}
});
И конечная точка сервера, использующая multer:
Код: Выделить всё
api.post('/myapi', upload.single('image'), function (req, res, next) { myapp.Handle(req, res, next); });
Я могу использовать либо multer, либо экспресс-загрузку файлов. Здесь вызывает беспокойство встроенный скрипт в шаблоне PUG. Как я могу отправить ОБА текстовое сообщение и загрузить файл в одном запросе на публикацию?
Подробнее здесь:
https://stackoverflow.com/questions/791 ... to-backend
1730617961
Anonymous
У меня есть следующий шаблон PUG: [code]extends layout block append head link(type='stylesheet', href='/stylesheets/mystyle.css', rel='stylesheet') block layout-content #chat-app #chat.has-text-centered h1.title Welcome to #{title} section.section.chat-container .container .columns .box.column.is-full h2.title Messages .chat-messages.has-text-left p#messages form#chatForm(action='/api/gemini', method='post', enctype="multipart/form-data") .field.has-addons p.control.is-expanded input(class='input', id='prompt', type='text', placeholder='How can I help?') p.control input(class='input', id='image', type='file') p.control input(class='button is-success', type='submit', value='Post') script. document.querySelector("#chatForm").addEventListener("submit", async (e) => { e.preventDefault(); const prompt = document.querySelector("#prompt").value; if (prompt.trim()) { const response = await fetch('/api/myapi', { method: 'POST', headers: { 'Content-Type': 'multipart/form-data' }, body: JSON.stringify({, prompt:, prompt}) }); const data = await response.json(); document.querySelector("#messages").innerHTML += ` [b]You:[/b] ${prompt} `; document.querySelector("#messages").innerHTML += ` [b]Server:[/b] ${data.message} `; document.querySelector("#prompt").value = ''; } }); [/code] И конечная точка сервера, использующая multer: [code]api.post('/myapi', upload.single('image'), function (req, res, next) { myapp.Handle(req, res, next); }); [/code] Я могу использовать либо multer, либо экспресс-загрузку файлов. Здесь вызывает беспокойство встроенный скрипт в шаблоне PUG. Как я могу отправить ОБА текстовое сообщение и загрузить файл в одном запросе на публикацию? Подробнее здесь: [url]https://stackoverflow.com/questions/79152211/node-js-pug-post-both-text-and-image-to-backend[/url]