1

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の場合にのみアクセスできると言われていますが、境界の問題が発生します

それについての助けは大歓迎です

4

2 に答える 2

0

このコードを記述する代わりに、 filepicker.ioを使用することもできます。これにより、独自の s3 バケットに接続できます。ファイルが保存されると、S3 URL を含むコールバックが返されます。その URL をノード API に渡すだけで保存できます。これを使用して、ファイルのアップロードを処理するための余分なサーバー コードを作成する必要がなくなりました。追加のボーナス、画像でこれを行う必要があり、ユーザーが画像を編集できるようにする場合は、画像をローカルで編集できるAviaryを使用できます。その後、保存できる別の s3 URL を取得します。あなたのサーバーに..

于 2013-06-21T21:18:51.517 に答える