0

i'm creating WP app with HTML, CSS and Javascripts, now i have a question: - I using openPicker.pickSingleFileAndContinue() to pick a picture from library and i create a file from it:

var file = MSApp.createFileFromStorageFile(filePicked);

I use console.log(file), it print:

[object File]
   {
      [functions]: ,
      __proto__: { },
      constructor: { },
      lastModifiedDate: [date] Thu Apr 02 2015 16:44:58,
      name: "WP_20150402_001.jpg",
      size: 1048405,
      type: "image/jpeg"
   }

Now, my question is: How can i convert it to JSON file with format:

{file:
 [
  {
    modified: date] Thu Apr 02 2015 16:44:58,
    name: "WP_20150402_001.jpg",
    size: 1048405,
    type: "image/jpeg"
  }
 ]
}

Tks for read, plz help me.

2
  • If I am correct you want to convert this object instance to a JSON string you can write to the filesystem? Commented Apr 2, 2015 at 10:46
  • Do you have control over File constructor or is it some native one? However in any case you will be able to implement toJson method. Commented Apr 2, 2015 at 10:55

1 Answer 1

1
var file = MSApp.createFileFromStorageFile(filePicked);

// Create POJO structured like the way we want.
var fileData = {
  file: {
    modified: file.lastModifiedDate,
    name: file.name,
    size: file.size,
    type: file.type
  }
}

// Convert data to JSON string.
var serializedData = JSON.stringify(fileData);

serializedData should now contain something like the following value:

"{"file":{"lastModifiedDate":"2015-04-02T10:52:31.993Z","name":"WP_20150402_001.jpg","size":1048405,"type":"image/jpeg"}}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.