already read file(s) from input node into string(s)

This commit is contained in:
Javier Abadia 2014-04-05 15:45:27 +02:00
parent f0f50a354e
commit 0970911db0

View File

@ -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<n; i++)
{
var file = inputNode.files[i];
console.log("reading file", file.name);
var reader = new FileReader();
// closure to keep 'file' value
reader.onload = (function(theFile){
return function(evt) {
console.log(evt);
files.push({
name: theFile.name,
type: theFile.type,
content: evt.target.result,
size: theFile.size
});
pendingFiles--;
if( pendingFiles == 0)
return callback(files);
}
}(file));
reader.readAsBinaryString(file);
};
});
}
layer.deleteAttachments = function(objectId,attachmentsIds,callback,errback){
if( self.getOnlineStatus() == self.ONLINE)
{