如何编写Node.js表达API进行文件上传? [英] How to write a Node.js express API for file uploading?

查看:176
本文介绍了如何编写Node.js表达API进行文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网上有很多关于Node.js Express框架文件上传的例子。但他们中的大多数人都在使用 multer 。所有这些都是从表单加载文件。

There are a lot of examples online regarding with file uploading for Node.js Express framework. But most of them are using multer. All of them are loading file from a form.

但我的情况有所不同。我的应用程序将从手机中选择一张图像并上传到服务器(通过使用Ionic中的cordova-file-transfer插件)。在这种情况下,我根本没有表格。所以没有req.files。有什么建议吗?谢谢。

But my scenario is different. My app will pick an image from mobile phone and upload to server (by using cordova-file-transfer plugin in Ionic). In this case, I don't have a form at all. So that there is no req.files. Any suggestion? Thanks.

PS:
这是我服务器中的日志记录我的http标头:

P.S: Here is the log in my server logs my http header:

{ host: 'localhost:3000',
  'x-requested-with': 'XMLHttpRequest',
  accept: '*/*',
  'content-type': 'multipart/form-data; boundary=+++++org.apache.cordova.formBoundary',
  'content-length': '23394',
  'accept-language': 'en-us',
  'accept-encoding': 'gzip, deflate',
  connection: 'keep-alive',
  'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13C75 (2079040640)' }

我的服务器代码:

  app.post('/', multer({dest:'./uploads/'}).single('upl'),(req,res) => {
    console.dir(req.headers)
    console.dir(req.body)
    res.status(204).end()
  })

在我的情况下,显然没有定义'upl'。

Apparently 'upl' is not defined in my case.

推荐答案

如果有人有同样的问题,这是我的完整源代码和解释。

In case somebody have the same issue, here is my full source code and explanation.

使用cordova-file-transfer插件进行上传的离子应用程序从照片库到node.js快递服务器的广告文件。

An ionic application which is using cordova-file-transfer plugin to upload file from photo library to a node.js express server.

var express = require('express')
var multer = require('multer')
var bodyParser = require('body-parser')
var path = require('path')

var app = express()

// settings
app.set('views', path.join(__dirname,'views'))
app.set('view engine','jade')

// middleware
app.use(bodyParser.json())

// route
app.get('/', (req,res) => {
  res.render('index')
})

app.post('/', multer({dest:'./uploads/'}).single('upl'),(req,res) => {
  console.log(req.body)
  console.log(req.file)
  res.status(204).end()
})

// start
var server = app.listen(3000, () => {
  console.log('Started at port ' + server.address().port)
})



我的客户端代码 上传()



My client code upload():

upload() {

var options = new FileUploadOptions()
options.fileKey = "upl";              // this equal to <input type="file" id="upl">
options.fileName = 'test.jpg';
options.mimeType = "image/jpg";
options.chunkedMode = false;
options.params = {'directory' : 'uploads', 'fileName': 'test.jpg'};

var ft = new FileTransfer()
ft.upload(this.thumbnail, encodeURI('http://localhost:3000/'),
  (res) => {
    console.log("Code = " + res.responseCode)
    console.log("Response = " + res.response)
    console.log("Sent = " + res.bytesSent)
  },(error) => {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
  },options)
}

this.thumbnail是我的文件路径,例如在我的ios中,路径类似于:

this.thumbnail is my file path, for example in my ios, the path is something like:

file:/// Users / myusername / Library / Developer / CoreSimulator / Devices / 81B513B7-AE34-4911-A9C9-57E293957BEC / data / Containers / Data / Application / C9A0BE15-EA4A-4DD8- 9E75-BC960ECF50B7 / tmp / cdv_photo_016.jpg

file:///Users/myusername/Library/Developer/CoreSimulator/Devices/81B513B7-AE34-4911-A9C9-57E293957BEC/data/Containers/Data/Application/C9A0BE15-EA4A-4DD8-9E75-BC960ECF50B7/tmp/cdv_photo_016.jpg

这篇关于如何编写Node.js表达API进行文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆