jqueryを使用して、express..を使用してnode.jsにファイルをアップロードしようとしています。
私のクライアント側のコードは次のとおりです。
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.')
},
})
}
マルチパート/フォームデータを使用すると、境界に関するエラーが表示されるため、コンテンツタイプなしで投稿フォームを使用しました..
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の場合にのみアクセスできると言われていますが、境界の問題が発生します
それについての助けは大歓迎です