5

Is it possible to run a check on a key for the formdata object? I would like to know if a key has already been assigned a value.

tried something like this with negative results

data=new FormData();
if(!data.key)
data.append(key,somevalue);

addition question is the nature of a double assignment to rewrite the original value?

Alex_Nabu
  • 311
  • 3
  • 11
  • See this question: http://stackoverflow.com/questions/7752188/formdata-appendkey-value-is-not-working – Tony Feb 19 '14 at 09:48
  • @tony Ok i can appreciate what the browser platform is trying to do with security I have an idea of what would be a suitable work around based off of your link. Why not why not give an example of putting it in a wrapper object that records the kv pairs and I'll accept it as an answer for reference – Alex_Nabu Feb 19 '14 at 16:41

1 Answers1

3

Things are changing and nowadays you can check if key exits using get function.

Original answer

As we already discussed in comments, browser hides data which is stored in FormData object due to security reasons. There is one workaround which helps to preview its data in developer console which is described here: FormData.append("key", "value") is not working

The only way to have access to such data in code is to use own wrapping object, which supports appending data, getting values and converting to FormData. It could be an object like this:

function FormDataUnsafe() {
    this.dict = {};
};

FormDataUnsafe.prototype.append = function(key, value) {
    this.dict[key] = value;
};

FormDataUnsafe.prototype.contains = function(key) {
    return this.dict.hasOwnProperty(key);
};

FormDataUnsafe.prototype.getValue = function(key) {
    return this.dict[key];
};

FormDataUnsafe.prototype.valueOf = function() {
    var fd = new FormData();
    for(var key in this.dict) {
        if (this.dict.hasOwnProperty(key))
            fd.append(key, this.dict[key]);
    }

    return fd;
};

FormDataUnsafe.prototype.safe = function() {
    return this.valueOf();
};

Usage:

var xhr = new XMLHttpRequest;
xhr.open('POST', '/', true);
xhr.send(data.safe());  // convertion here

Demo

Tony
  • 7,345
  • 3
  • 26
  • 34
  • for a little more insight for those in the same situation. the recommended solution is to record what you put in in some form or other(answer does that in the form of a wrapping object). and use that reference to say what you already put in hence if the key already exists – Alex_Nabu Mar 10 '14 at 05:04
  • At least, can I check if `formdata` object has a file inside? – MarceloBarbosa Feb 11 '15 at 17:00
  • Could you give some details about "browser hides data which is stored in FormData object due to security reasons." even if a link is preferred. – Ye Shiqing Dec 07 '17 at 03:00
  • @PageYe this answer is very old and many things changed since that time, so it seems now it is not a security issue anymore. – Tony Dec 07 '17 at 07:11
  • @Tony I just want to know the reason why should browser hides data instead of showing the properties directly? – Ye Shiqing Dec 07 '17 at 09:10