From 0970911db0f6bd9422e45b676d94c7ebfefb26d0 Mon Sep 17 00:00:00 2001 From: Javier Abadia Date: Sat, 5 Apr 2014 15:45:27 +0200 Subject: [PATCH] already read file(s) from input node into string(s) --- lib/edit/offlineFeaturesManager.js | 97 +++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 3 deletions(-) diff --git a/lib/edit/offlineFeaturesManager.js b/lib/edit/offlineFeaturesManager.js index 6522dcf..f1be2f6 100644 --- a/lib/edit/offlineFeaturesManager.js +++ b/lib/edit/offlineFeaturesManager.js @@ -9,6 +9,7 @@ define([ "dojo/promise/all", "dojo/_base/declare", "dojo/_base/lang", + "dojo/_base/array", "dojo/dom-attr", "dojo/dom-style", @@ -20,7 +21,7 @@ define([ "esri/request"], function(editsStore, AttachmentsStore, - Evented,Deferred,all,declare,lang,domAttr,domStyle, + Evented,Deferred,all,declare,lang,array,domAttr,domStyle, GraphicsLayer,Graphic,SimpleMarkerSymbol,SimpleLineSymbol,SimpleFillSymbol, esriRequest) { @@ -42,6 +43,35 @@ define([ ATTACHMENTS_SENT: 'attachments-sent' }, + _checkFileAPIs: function() + { + if( window.File && window.FileReader && window.FileList && window.Blob ) + { + console.log("All APIs supported!"); + + if(!XMLHttpRequest.prototype.sendAsBinary ) + { + // https://code.google.com/p/chromium/issues/detail?id=35705#c40 + XMLHttpRequest.prototype.sendAsBinary = function(datastr) + { + function byteValue(x) { + return x.charCodeAt(0) & 0xff; + } + var ords = Array.prototype.map.call(datastr, byteValue); + var ui8a = new Uint8Array(ords); + this.send(ui8a.buffer); + } + console.log("extending XMLHttpRequest"); + } + return true; + } + else + { + alert('The File APIs are not fully supported in this browser.'); + return false; + } + }, + /** * Overrides a feature layer. * @param layer @@ -63,6 +93,11 @@ define([ layer._queryAttachmentInfos = layer.queryAttachmentInfos; layer._deleteAttachments = layer.deleteAttachments; + if( !this._checkFileAPIs()) + { + return callback(false, "File APIs not supported"); + } + try { layer.attachmentsStore = new AttachmentsStore(); @@ -84,6 +119,8 @@ define([ 2. add a new attachment to a new feature 3. remove an attachment that is already in the server... how do we know about it? 4. remove an attachment that is not in the server yet + 5. update an existing attachment to an existing feature, how do we know about it? + 6. update a new attachment concerns: - should the key in the db be the objectId or the attachmentId? @@ -152,11 +189,65 @@ define([ // c) what should be the key in the db? objectid? attachmentid? problem is that when we are offline // we don't know about attachmentids and other attachments that can be attached to the same feature // we could use negative attachmentids, as we do with feature objectids - if(typeof objectId !== "undefined" || objectId.length > 0 || objectId !== null){ - } + console.assert(objectId); + + console.log(formNode); + + // get input node(s) + // read file from input into variable + this._readFilesFromForm(formNode, function(files) + { + // callback called once for each input[type=file] node in formNode + console.log(files); + // store the file(s) + }); + } } + // callback called once for each input[type=file] node in formNode + layer._readFilesFromForm = function(formNode, callback) + { + var inputNodes = array.filter(formNode.elements, function(node) { return node.type == "file"}); + inputNodes.forEach(function(inputNode) + { + var files = []; + var pendingFiles = inputNode.files.length + var i, n = inputNode.files.length; + + if( pendingFiles == 0) + return callback(files); + + for(i=0;i