recherche

Maison  >  Questions et réponses  >  le corps du texte

html5 - téléchargement de fichiers Multer

Utilisez multer pour télécharger des fichiers. Il n'y a aucun fichier après le téléchargement et req.files n'est pas défini.

app.js

var fileupload=require('./routes/ajax/fileupload.js');
app.use('/upload',fileupload);

fileupload.js

var express = require('express');
const app = new express();
const multer = require('multer');
var upload=multer({dest:__dirname+'/upload'});

app.post('/',upload.any(),(req,resp,next)=>{
    resp.send('success!');
});

module.exports=app;

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>ajax demo3 formData</title>
    </head>
    <body>
        <h3>Entropy text/plain</h3>
        <p>the txet/plain is not supported</p>
        <h3>Entropy multipart/form-data</h3>
        <form action="/upload" entropy="multipart/form-data" method="post" onsubmit="AjaxSubmit(this);return false;">
            <fieldset>
            <p>
                firstname:<input type="text" name="firstname"/><br/>
                lastname:<input type="text" name="lastname"/></br>
                Sex:
                <input id="sex_male" type="radio" name="sex" value="male" /><label for="sex_male">male</label><br/>
                <input id="sex_female" type="radio" name="sex" value="female"><label for="sex_female">female</label><br/>
                password:<input type="password" name="secret"><br/>
                What your pereform:
                <select name="image_type">
                    <option>Books</option>
                    <option>Senima</option>
                    <option>TV</option>
                </select>
            </p>

            <p>
                input type your photo
                <input type="file" multiple="multiple" name="photos[]"/>
            </p>

            <p>
                <input id="vehicle_bike" type="checkbox" name="vehicle[]" value="Bike"/> <label for="vehicle_bike">I have a bike</label><br />
                <input id="vehicle_car" type="checkbox" name="vehicle[]" value="Car"/><label for="vehicle_car">I have a car</label><br/>
            </p>
            <p>
                <textarea type="description" cols="50" rows="8"></textarea>
            </p>
            <p>
                <input type="submit" Value="Submit">
            </p>
            </fieldset>


            <script type="text/javascript">
                function AjaxSubmit(oFormElement){
                    if(!oFormElement.action){
                        return ;
                    }


                    var xhr = new XMLHttpRequest();

                    if(oFormElement.method="post"){

                        xhr.open('post',oFormElement.action);
                        xhr.send(new FormData(oFormElemet));

                    }else{

                        var oField,sFileType,nFile,sSearch="";
                        for(var nItem=0;nItem<oFormElement.length;nItem++){
                            oField=oFormElement[nItem];
                            if(!oField.hasAttribute('name')){
                                continue;
                            }
                            
                            sFieldType=oField.nodeName.toUpperCase()==='INPUT'?oField.type.toUpperCase():'TEXT';

                            if(sFieldType==='FILE'){
                                for(var nFile=0;nFile<oField.files.length;sSearch+='&'+escape(oField.name)+'='+escape(oField.files[nFile++].name));

                            }else{
                                sSearch+='&'+escape(oField.name)+'='+escape(oField.value);
                            }


                        }

                        xhr.open("get", oFormElement.action.replace(/(?:\?.*)?$/, sSearch.replace(/^&/, "?")), true);
                        xhr.send(null);
                    }
                }

            </script>

        </form>
        <!-- fieldset textarea type="description" checkbox radio multiple -->
    </body>
</html>
某草草某草草2724 Il y a quelques jours778

répondre à tous(1)je répondrai

  • 伊谢尔伦

    伊谢尔伦2017-05-31 10:43:01

    app.post('/upload',upload.any(),(req,resp,next)=>{
        // req.files
        resp.send('success!');
    });

    répondre
    0
  • Annulerrépondre