文件上传使用快递4.15无法正常工作

时间:2017-06-26 15:59:08

标签: javascript node.js express

我第一次使用快递,在上传文件时遇到了一些问题。我已经关注了许多教程,但没有人工作过。我只是一个空页面,没有上传文件

index1.jade

cmake_minimum_required(VERSION 2.8)                     #Specify the minimum CM$
project(gaussian)                                                              $
find_package(CUDA REQUIRED)                                     #find the CUDA $
find_package(ITK REQUIRED)

include( ${ITK_USE_FILE} )
#message("Debug: ITK ${ITK_DIR}")

include_directories(${CUDA_INCLUDE_DIRS})   #Specify the CUDA include direc$


add_executable(gaussian source/main.cu)                    #create an executabl$

#specify any additional libraries here (CUFFT and CUBLAS can be useful)
target_link_libraries(gaussian ${CUDA_cufft_LIBRARY} ${CUDA_cublas_LIBRARY} ${I$

app.js

#include <fstream>
#include <cuda.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctime>
#include <cuda_runtime_api.h>
#include <cufft.h>
#include "itkImage.h"

using namespace std;
static void HandleError( cudaError_t err, const char *file, int line ) 
{
        if (err != cudaSuccess) 
                cout<<cudaGetErrorString(err)<<" in "<< file <<" at line "<< line<<endl;
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))
int main(int argc, char* argv[])
{
        typedef itk::Image< unsigned short, 3 > ImageType;
        ImageType::Pointer image = ImageType::New();
        cout << "ITK Hello World !" << endl;
        int nDevices;
        cout<<"DEVICE SPECIFICATIONS: "<<endl<<endl;
        HANDLE_ERROR(cudaGetDeviceCount(&nDevices)); 
        return 0;
}

路线/ index.js

doctype html
html(lang='en')
head
    title
    meta(charset='UTF-8')
body
    form(role="form", action="/", method="post", enctype="multipart/form-data")
    div(class="form-group")
        label(for="onixfile") Upload ONIX File        
        input(type="file", name="onixfile", id="onixfile")
        input(type="submit")

2 个答案:

答案 0 :(得分:0)

在你的route / index.js

中执行此操作
var upload = multer({ dest: 'uploads/' });

router.post('/upload', upload.single('file'), function(req, res){

    var file = '/' + req.file.filename;
    var fs = require('fs');
    fs.open("./uploads",'r',function(err, fd){
    fs.readFile(file ,function(err, data){
      if(err) throw err;
      fs.writeFile("./uploads/"+req.file.filename, data), function(err) {
        if(err) {
            console.log(err);
        }else {
            console.log("Done");
        }
      }
     });
    });


});

这应该将文件上传到/ uploads目录中的应用程序文件系统。

答案 1 :(得分:0)

确保你必须放置这些html&amp;将js文件存入解决方案目录中的uploads文件夹。在代码下面检查它。

Index.html代码:

    <form id        =  "uploadForm"
         enctype   =  "multipart/form-data"
         action    =  "/api/photo"
         method    =  "post"
    >
    <input type="file" name="userPhoto" />
    <input type="submit" value="Upload Image" id="upload" name="submit">
    </form>

Server.js代码:

var express = require("express");
var multer = require('multer');

var app = express();

var storage = multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, './uploads');
    },
    filename: function (req, file, callback) {
        var splitted = file.originalname.split('.');
        var format = splitted[splitted.length - 1];
        callback(null, file.fieldname + '-' + Date.now() + '.' + format);
    }
});
var upload = multer({ storage: storage }).single('userPhoto');

app.get('/', function (req, res) {
    res.sendFile(__dirname + "/index.html");
});

app.post('/api/photo', function (req, res) {
    upload(req, res, function (err) {
        if (err) {
            return res.end("Error uploading file.");
        }
        res.send("File is uploaded");
    });
});

app.listen(3000, function () {
    console.log("Working on port 3000");
});