write.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. /*
  22. * write
  23. *
  24. * IN:
  25. * args
  26. * 0 - URL of file to write
  27. * 1 - data to write
  28. * 2 - offset
  29. * 3 - isBinary
  30. * OUT:
  31. * success - bytes written
  32. * fail - FileError
  33. */
  34. var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
  35. requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
  36. module.exports = function (success, fail, args) {
  37. var uri = args[0],
  38. data = args[1],
  39. offset = args[2],
  40. //isBinary = args[3],
  41. onSuccess = function (data) {
  42. if (typeof success === 'function') {
  43. success(data.loaded);
  44. }
  45. },
  46. onFail = function (error) {
  47. if (typeof fail === 'function') {
  48. if (error && error.code) {
  49. fail(error.code);
  50. } else if (error && error.target && error.target.code) {
  51. fail(error.target.code);
  52. } else {
  53. fail(error);
  54. }
  55. }
  56. };
  57. resolve(function (fs) {
  58. requestAnimationFrame(function () {
  59. fs.nativeEntry.createWriter(function (writer) {
  60. var blob = new Blob([data]);
  61. if (offset) {
  62. writer.seek(offset);
  63. }
  64. writer.onwriteend = onSuccess;
  65. writer.onerror = onFail;
  66. writer.write(blob);
  67. }, onFail);
  68. });
  69. }, fail, [uri, { create: true }]);
  70. };