Where to Save Files in Monaca

Asked 2 years ago, Updated 2 years ago, 81 views

I am creating an iPad app with Monaca.Creating an app to download files.

If you run the following code on the debugger of the actual machine,

I saved the
 file to /path/to/downlards.

But where is it actually stored?

Also, how do I load the files I want to save? (Does this app work with a debugger?)

<!DOCTYPE HTML>
<html>
<head>
    <metacharset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scale=no">
    <script src="components/loader.js"></script>
    <link rel="stylesheet" href="components/loader.css">
    <link rel="stylesheet" href="css/style.css">
    <script>
      var directoryEntry;

  document.addEventListener('device ready', init, false);
  function init() {
  window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, getFilesFromDirectory, fail);
  // window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, getFilesFromDirectory, fail);
  document.getElementById('download').addEventListener('click', downloadFile, false);
  }

  function getFilesFromDirectory(fileSystem){
  directoryEntry='cdvfile://localhost/persistent/path/to/downloads/';
  var directoryReader = directoryEntry.createReader();
  directoryReader.readEntries (putFileName, fail);
  }

  function putFileName(entries){
      alert(entries.length);
  for (index=0; index<entries.length;index++) {
      alert(entries [index].fullPath);
  if(entries[index].isFile){
  var listItem= document.createElement('li');
  listItem.textContent=entries [index].name;
  listItem.title=entries [index].fullPath;
  listItem.addEventListener('click', uploadFile, false);
  document.getElementById('fileList').appendChild(listItem);
  }
  }
  }

  function downloadFile(){
  // Create FileTransfer Object
  var fileTransfer = new FileTransfer();

  // EncodeURI encodes the download destination URI

  varuri=encodeURI('http://image.gihyo.co.jp/assets/templates/gihyojp2007/image/gihyojp_logo.png');
  alert(uri);
  // Name of file to save
  varfileName='gihyojp_logo.png';

  // Specify the destination as full path

  var path='cdvfile://localhost/persistent/path/to/downloads/';
  alert(path);
  // Perform File Download
  fileTransfer.download(uri, path, downloadSuccess, downloadFail);
  }

  function downloadSuccess(fileEntry){
  alert('Saved file to '+fileEntry.fullPath+'.');
  location.reload();
  }

  function downloadFail(fileTransferError) {
  error=';

  switch(fileTransferError.code){
  caseFileTransferError.FILE_NOT_FOUND_ERR:
  error='No file URL specified for second argument';
  break;
  caseFileTransferError.INVALID_URL_ERR:
  error='An invalid file path is specified in the second argument';
  break;
  caseFileTransferError.CONNECTION_ERR:
  error = 'Connection Error'
  break;
  default: 
  error = 'Undefined error';
  break;
  }

  alert(error+'\nhttp_status:'+fileTransferError.http_status);
  }

  function uploadFile(event){
  // Create FileTransfer Object
  var fileTransfer = new FileTransfer();

  // Encode URI to upload destination
  varuri=encodeURI('http://(hostname)/receive.php');

  // Create FileUploadOptions object and specify information when sending
  var uploadOptions = new FileUploadOptions();
  uploadOptions.fileKey='file';
  uploadOptions.fileName=event.target.textContent;

  // Perform file upload
 fileTransfer.upload(event.target.title,uri,uploadSuccess,uploadFail,uploadOptions);
 }

 function uploadSuccess(uploadResult){
 var result;

 // Parse the JSON returned from the upload destination (receive.php)
 if('Android'===device.platform){
 result=JSON.parse(uploadResult.response);
 } else{ 
 // Decode with decodeURI because iOS is an encoded string.
 result=JSON.parse(decodeURI(uploadResult.response)));
 }

 if(0===result.errorCode){
 alert('File upload succeeded\nbytesSent:'+uploadResult.bytesSent+'\nresponseCode:'+uploadResult.responseCode);
 } else{
 alert('File upload failed\n'+result.message+'\nbytesSent:'+uploadResult.bytesSent+'\nresponseCode:'+uploadResult.responseCode);
 }
 }

 function uploadFail(fileTransferError) {
 error=';

 switch(fileTransferError.code){
 caseFileTransferError.FILE_NOT_FOUND_ERR:
 error='Files specified for first argument not found';
 break;
 caseFileTransferError.INVALID_URL_ERR:
 error='An invalid URI is specified in the second argument';
 break;
 caseFileTransferError.CONNECTION_ERR:
 error = 'Connection Error'
 break;
 default: 
 error = 'Undefined error';
 break;
 }

 alert(error+'\nhttp_status:'+fileTransferError.http_status);
 }

 function failure(error){
 alert('An error has occurred.Error code: '+error.code);
 }
    </script>
</head>
<body>
    <div class="app">
        <h1>File API Test</h1>
        <p>
        <input id="download" type="button" value="download gihyo logo">
        </p>
        <ulid="fileList"></ul>
    </div>
</body>
</html>

ios monaca

2022-09-30 18:14

1 Answers

Answer where to save the file.

cdvfile://localhost/persistent seems to point to an app-specific data folder.

Monaca Docs::File Plug-in
See Persistent Storage in ↑.

If you have not added the following definition to config.ios.xml, I think it is {Application Specific Folder}/Documents.

//config.ios.xml
<preference name="iosPersistentFileLocation" value="Library"/>

If you install i-FunBox, you can easily access the app-specific folders.


2022-09-30 18:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.