简体   繁体   中英

How to make request.files JSON iterate-able

Question: Do I need to parse this? If so, how? If not, how can I iterate through this?

Issue: I am uploading two images from iOS to server. The files come in under request.files and looks this:

{ files:
[ { 
    name: 'file0',
    data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 48 00 48 00 00 ff e1 00 58 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 02 01 12 00 03 00 00 00 01 00 06 ... >,
    encoding: '7bit',
    mimetype: 'image/jpg',
    mv: [Function: mv] },
  { 
    name: 'file1',
    data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 48 00 48 00 00 ff e1 00 58 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 02 01 12 00 03 00 00 00 01 00 06 ... >,
    encoding: '7bit',
    mimetype: 'image/jpg',
    mv: [Function: mv] 
  } 
] }

I am trying to access each object and insert data in S3 and other information into Postgres. The issue is that I cannot parse this obj.

Object.keys(request.files).length // = 1, the length is clearly 2
request.files.length // = undefined
rqeuest.files.file1 // undefined

If I edit this json in a text editor to have ' ' around all words, I can iterate through the object fine. How can I work with this json Object?

It looks like request.files was already parsed from JSON and is a Javascript object.

If that is true, to get at the files property value, you need to use request.files.files .

Note: request.files.files.file1 would still not work, since request.files.files is an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM