How can we add file in project directory using javascript onclick?

exe :

How can we possibly add a file in our project directory using the javascript onclick is it possible? Need your help guys. I have done many research about this and nothing helped. If possible I dont want to add enctype multipart/form-data in my form because it wont allow me to store my data to mysql.

<form class="add-music-form" action="/save" method="POST">
    <div class="form-group">
        <div class="song-feaured-image">
            <input style="display: none;" type="file" accept="image/*" id="file" name="featured_img"  onchange="loadFile(event)" required>
            <img id="output" class="image-preview" width="130" src="/img/rock-on.jpg" />
            <label for="file" class="attach_file" style="cursor: pointer">
            <i class="material-icons">attach_file</i></label>
        </div>
    </div>

    <div class="form-group">
        <input type="text" class="form-control" placeholder="Title" id="title" name="title" required>
    </div>

    <div class="form-group">
        <input type="text" class="form-control" placeholder="Band name" id="band_name" name="band_name" required>
    </div>

    <div class="custom-file">
        <input type="file" class="custom-file-input" id="customFile" name="audio" required>
        <label class="custom-file-label" for="customFile">Select audio file</label>
    </div>
    <div class="mt-3">
        <button type="submit" class="btn btn-primary" onclick="submit()">Submit</button>
        <a onclick="cancel()" class="btn btn-default">Cancel</a>
    </div>
</form>

<script>
function submit() {
    let upload_img = document.getElementById("output");
    //--------------???----------------//
}
</script>

app.js

app.post('/save', (req, res) => {
    let sql = "INSERT INTO `music`(`featured_img`, `title`, `band_name`, `audio`) VALUES ('" + req.body.featured_img + "', '"+req.body.title+"', '"+req.body.band_name+"', '"+req.body.audio+"')";
    let query = connection.query(sql, (err, results) => {
        if(err) throw err;
        res.redirect('/');
    });
});
user1855085 :

(Note: I had trouble getting this working with EJS)

/client/index.html

<form class="add-music-form" action="/save" method="POST">
    <div class="form-group">
        <div class="song-featured-image">
            <input style="display: none;" type="file" accept="image/*" id="file" name="featured_img" required>
            <img id="output" class="image-preview" width="130" src="/img/rock-on.jpg" />
            <label for="file" class="attach_file" style="cursor: pointer">
            <i class="material-icons">attach_file</i></label>
        </div>
    </div>
    <div class="mt-3">
        <button type="submit" class="btn btn-primary">Submit</button>
        <a onclick="cancel()" class="btn btn-default">Cancel</a>
        </div>
</form>

app.js

var express = require('express');
const fileUpload = require('express-fileupload');
var app = express();

app.use(fileUpload());
app.use(express.static('client'));

app.listen(80, function () {
    console.log("Server loaded.");
});

app.post('/save', (req, res) => {
    let fileName = req.files.featured_img.name;
    let data = req.files.featured_img.data;
    console.log(fileName);
    console.log(data);
    // Do SQL
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to import external javascript file in the main.js, can we access the external file using javascript

From Dev

How can we edit generated config file with my project specific parameters in Doxygen using command line?

From Dev

how can we create a config file from a nested directory in python?

From Dev

How can we add new rows at the top of the CSV file using powershell script?

From Dev

How can we add a download attribute to all the <a> tags present inside <body></body> tag (using JavaScript)?

From Dev

how can I add onclick or eventListener while appending a button using javascript

From Dev

How can we use splint using cmake for large project

From Dev

How can we know the content of a file from a Javascript File object?

From Dev

can we build controller and model classes into seperate jar file and add that jar file as a dependency to another project

From Dev

how we can add two event listeners click and keydown in javascript

From Dev

How can we add `Button` and `TextField` by using `SwiftUI`

From Dev

How can we add image in UITableView section header using swift?

From Dev

in webview how we can access HTML file with javascript CSS styles

From Dev

How can we convert .wav file to text by using pocketsphinx?

From Dev

How can I sign an XLAM file using a certificate we purchased?

From Dev

How can we write a text file from variable using python?

From Dev

how can we sign .exe file using .pfx certificate in centos

From Dev

How can we update parent header in json file using python?

From Dev

How can I add a onclick method using js_of_ocaml?

From Java

Can we specify directory path with the property file while using ResourceBundle class in Java?

From Dev

How to add popup after page reload using onclick javascript

From Dev

How to add a onclick event to a table cell using javascript?

From Dev

How to dynamicallly add, delete node in list using onclick javascript

From Dev

how we can refresh or reset a form using javascript funcion

From Dev

how we can get value from nprinting variable using javascript

From Dev

How to add external javascript file into all html files in directory?

From Dev

how can we add project number from variable in terraform gcp resource iam binding

From Dev

How to add a binary data file in the same directory as of exe using pyinstaller?

From Dev

How can I add file to project in Xcode 9?

Related Related

  1. 1

    How to import external javascript file in the main.js, can we access the external file using javascript

  2. 2

    How can we edit generated config file with my project specific parameters in Doxygen using command line?

  3. 3

    how can we create a config file from a nested directory in python?

  4. 4

    How can we add new rows at the top of the CSV file using powershell script?

  5. 5

    How can we add a download attribute to all the <a> tags present inside <body></body> tag (using JavaScript)?

  6. 6

    how can I add onclick or eventListener while appending a button using javascript

  7. 7

    How can we use splint using cmake for large project

  8. 8

    How can we know the content of a file from a Javascript File object?

  9. 9

    can we build controller and model classes into seperate jar file and add that jar file as a dependency to another project

  10. 10

    how we can add two event listeners click and keydown in javascript

  11. 11

    How can we add `Button` and `TextField` by using `SwiftUI`

  12. 12

    How can we add image in UITableView section header using swift?

  13. 13

    in webview how we can access HTML file with javascript CSS styles

  14. 14

    How can we convert .wav file to text by using pocketsphinx?

  15. 15

    How can I sign an XLAM file using a certificate we purchased?

  16. 16

    How can we write a text file from variable using python?

  17. 17

    how can we sign .exe file using .pfx certificate in centos

  18. 18

    How can we update parent header in json file using python?

  19. 19

    How can I add a onclick method using js_of_ocaml?

  20. 20

    Can we specify directory path with the property file while using ResourceBundle class in Java?

  21. 21

    How to add popup after page reload using onclick javascript

  22. 22

    How to add a onclick event to a table cell using javascript?

  23. 23

    How to dynamicallly add, delete node in list using onclick javascript

  24. 24

    how we can refresh or reset a form using javascript funcion

  25. 25

    how we can get value from nprinting variable using javascript

  26. 26

    How to add external javascript file into all html files in directory?

  27. 27

    how can we add project number from variable in terraform gcp resource iam binding

  28. 28

    How to add a binary data file in the same directory as of exe using pyinstaller?

  29. 29

    How can I add file to project in Xcode 9?

HotTag

Archive