【发布时间】:2013-05-22 02:24:15
【问题描述】:
我正在尝试使用 jquery 将文件从 Android 应用程序上传到 node.js 使用 express..
我的客户端代码是:
function uploadData(win) {
var padI = imagedata.length-1
while( '=' == imagedata[padI] ) {
padI--
}
var padding = imagedata.length - padI - 1
var user = load('user')
$.ajax({
url:'http://'+server+'/lifestream/api/user/'+user.username+'/upload',
type:'POST',
contentType: false,
processdata:false,
data:imagedata,
success:win,
error:function(err){
showalert('Upload','Could not upload picture.')
},
})
}
我使用了没有任何内容类型的 post 表单,因为如果我使用 multipart/form-data 它会显示关于边界的错误 ..
我使用 node.js 的服务器端代码是:
function upload(req,res) {
var picid=uuid()
console.log('Got here..' + __dirname)
//console.log('Image file is here ' + req.files.file.path)
// console.log('local name: ' + req.files.file.name)
var serverPath = __dirname+'/images/' + picid+'.jpg'
fs.rename(
req.files.file.path,
serverPath,
function(error) {
if (error) {
console.log('Error '+error)
res.contentType('text/plain')
res.send(JSON.stringify({error: 'Something went wrong saving to server'}))
return;
}
// delete the /tmp/xxxxxxxxx file created during download
fs.unlink(req.files.file.path, function() { })
res.send(picid)
}
)
}
当文件到达服务器时,它会报错 res.files.file is undefined ..
我搜索了很多论坛,他们说 res.files.file 仅在 contenttype 为 multipart/form-data 时才可以访问,但随后出现边界问题
非常感谢任何帮助
【问题讨论】:
-
我查看了那个网站,问题是它从浏览器生成客户端请求,所以它会包含边界变量,但是当我从 jquery ajax 生成它时会出错。跨度>