2017-11-29 1 views
0

그래서, 내가 B. NodeJS는 원격 서버에 로컬 컴퓨터에서 파일을 업로드

그리고 다음과 같은 코드 경로 A로부터 파일을 업로드 할 수 있어요 내 로컬 컴퓨터에서 작업했다

HTML

<form action="/fileupload" method="post" enctype="multipart/form-data"> 
    <input type="hidden" class="form-control" name="csrfmiddlewaretoken" value="{{_csrf}}"> 
    <input type="file" name="filetoupload"><br> 
    <input type="submit"> 
</form> 

NodeJS

app.post('/fileupload', function (req, res) { 
    var form = new formidable.IncomingForm(); 
    form.parse(req, function (err, fields, files) { 
     var oldpath = files.filetoupload.path; 
     var newpath = 'C:/Users/MyName/uploadtesterFile/' + files.filetoupload.name; 


     fs.rename(oldpath, newpath, function (err) { 
     console.log(err); 

     if (err) throw err; 
     res.redirect(req.get('referer')); 
     }); 
    }); 
}) 

은 C로 파일을 업로드 :/사용자/MyName로/uploadtesterFile/성공적를 하지만 원격 서버 경로으로 변경하면 오류, 교차 장치 링크가 허용되지 않고 뭔가가 반환됩니다.

enter image description here

는 참조 거기에 있습니까? 나는 W3Cschool 튜토리얼을 따라 가고있다.

답변

1

multer이라는 패키지가 있습니다. 멀티 파트 양식을 처리하고 데이터를 쉽게 업로드하는 미들웨어입니다. 또한 우리의 필요에 따라 구성 가능합니다.

var express = require('express') 
var multer = require('multer') 
var upload = multer({ dest: 'uploads/' }) 

var app = express() 

app.post('/profile', upload.single('avatar'), function (req, res, next) { 
    // req.file is the `avatar` file 
    // req.body will hold the text fields, if there were any 
}) 

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) { 
    // req.files is array of `photos` files 
    // req.body will contain the text fields, if there were any 
}) 

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]) 
app.post('/cool-profile', cpUpload, function (req, res, next) { 
    // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files 
    // 
    // e.g. 
    // req.files['avatar'][0] -> File 
    // req.files['gallery'] -> Array 
    // 
    // req.body will contain the text fields, if there were any 
}) 
+0

실무 예제가 있습니까? – anson920520

관련 문제