Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <template>
- <div class="question-form">
- <form name="ask-question"
- method="post"
- data-netlify="true"
- data-netlify-honeypot="bot-field"
- @submit.prevent="handleSubmit"
- enctype="multipart/form-data">
- <input type="hidden" name="form-name" value="ask-question" />
- <ul>
- <li>
- <label>
- Name:
- <input
- type="text"
- name="name"
- @input="ev => form.name = ev.target.value"
- >
- </label>
- </li>
- <li>
- <label>
- Upload:
- <input
- type="file"
- name="attach"
- ref="file"
- @change="addFile()"
- >
- </label>
- </li>
- </ul>
- <button type="submit" class="submit-button">Send</button>
- </form>
- </div>
- </template>
- <script>
- import axios from "axios"
- export default {
- data() {
- return {
- form: {
- name: "",
- attach: ""
- },
- };
- },
- methods: {
- addFile() {
- this.form.attach = this.$refs.file.files[0];
- },
- encode (data) {
- .map(
- key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`
- )
- .join("&"))
- return Object.keys(data)
- .map(
- key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`
- )
- .join("&");
- },
- handleSubmit() {
- const data = {
- "form-name": "ask-question",
- "name": this.form.name,
- "attach": this.form.attach
- }
- const axiosConfig = {
- header: { "Content-Type": "multipart/form-data" }
- };
- axios.post(
- "/",
- this.encode({
- data
- }),
- axiosConfig
- )
- .then(function (response) {
- console.log(response);
- })
- .catch(function (error) {
- console.log(error);
- });
- }
- }
- };
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement