FileItem.as 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package {
  2. import flash.net.FileReference;
  3. internal class FileItem
  4. {
  5. private static var file_id_sequence:Number = 0; // tracks the file id sequence
  6. private var postObject:Object;
  7. public var file_reference:FileReference;
  8. public var id:String;
  9. public var index:Number = -1;
  10. public var file_status:int = 0;
  11. private var js_object:Object;
  12. public static var FILE_STATUS_QUEUED:int = -1;
  13. public static var FILE_STATUS_IN_PROGRESS:int = -2;
  14. public static var FILE_STATUS_ERROR:int = -3;
  15. public static var FILE_STATUS_SUCCESS:int = -4;
  16. public static var FILE_STATUS_CANCELLED:int = -5;
  17. public static var FILE_STATUS_NEW:int = -6; // This file status should never be sent to JavaScript
  18. public function FileItem(file_reference:FileReference, control_id:String, index:Number)
  19. {
  20. this.postObject = {};
  21. this.file_reference = file_reference;
  22. this.id = control_id + "_" + (FileItem.file_id_sequence++);
  23. this.file_status = FileItem.FILE_STATUS_NEW;
  24. this.index = index;
  25. this.js_object = {
  26. id: this.id,
  27. index: this.index,
  28. post: this.GetPostObject()
  29. };
  30. // Cleanly attempt to retrieve the FileReference info
  31. // this can fail and so is wrapped in try..catch
  32. try {
  33. this.js_object.name = this.file_reference.name;
  34. this.js_object.size = this.file_reference.size;
  35. this.js_object.type = this.file_reference.type || "";
  36. this.js_object.creationdate = this.file_reference.creationDate || new Date(0);
  37. this.js_object.modificationdate = this.file_reference.modificationDate || new Date(0);
  38. } catch (ex:Error) {
  39. this.file_status = FileItem.FILE_STATUS_ERROR;
  40. }
  41. this.js_object.filestatus = this.file_status;
  42. }
  43. public function AddParam(name:String, value:String):void {
  44. this.postObject[name] = value;
  45. }
  46. public function RemoveParam(name:String):void {
  47. delete this.postObject[name];
  48. }
  49. public function GetPostObject(escape:Boolean = false):Object {
  50. if (escape) {
  51. var escapedPostObject:Object = { };
  52. for (var k:String in this.postObject) {
  53. if (this.postObject.hasOwnProperty(k)) {
  54. var escapedName:String = FileItem.EscapeParamName(k);
  55. escapedPostObject[escapedName] = this.postObject[k];
  56. }
  57. }
  58. return escapedPostObject;
  59. } else {
  60. return this.postObject;
  61. }
  62. }
  63. // Create the simply file object that is passed to the browser
  64. public function ToJavaScriptObject():Object {
  65. this.js_object.filestatus = this.file_status;
  66. this.js_object.post = this.GetPostObject(true);
  67. return this.js_object;
  68. }
  69. public function toString():String {
  70. return "FileItem - ID: " + this.id;
  71. }
  72. /*
  73. // The purpose of this function is to escape the property names so when Flash
  74. // passes them back to javascript they can be interpretted correctly.
  75. // ***They have to be unescaped again by JavaScript.**
  76. //
  77. // This works around a bug where Flash sends objects this way:
  78. // object.parametername = "value";
  79. // instead of
  80. // object["parametername"] = "value";
  81. // This can be a problem if the parameter name has characters that are not
  82. // allowed in JavaScript identifiers:
  83. // object.parameter.name! = "value";
  84. // does not work but,
  85. // object["parameter.name!"] = "value";
  86. // would have worked.
  87. */
  88. public static function EscapeParamName(name:String):String {
  89. name = name.replace(/[^a-z0-9_]/gi, FileItem.EscapeCharacter);
  90. name = name.replace(/^[0-9]/, FileItem.EscapeCharacter);
  91. return name;
  92. }
  93. public static function EscapeCharacter():String {
  94. return "$" + ("0000" + arguments[0].charCodeAt(0).toString(16)).substr(-4, 4);
  95. }
  96. }
  97. }