Search in sources :

Example 1 with File

use of com.gargoylesoftware.htmlunit.javascript.host.file.File in project htmlunit by HtmlUnit.

the class FormData method append.

/**
 * Appends a new value onto an existing key inside a {@code FormData} object,
 * or adds the key if it does not already exist.
 * @param name the name of the field whose data is contained in {@code value}
 * @param value the field's value
 * @param filename the filename reported to the server (optional)
 */
@JsxFunction
public void append(final String name, final Object value, final Object filename) {
    if (value instanceof File) {
        final File file = (File) value;
        String fileName = null;
        String contentType;
        if (filename instanceof String) {
            fileName = (String) filename;
        }
        contentType = file.getType();
        if (StringUtils.isEmpty(contentType)) {
            if (getBrowserVersion().hasFeature(JS_FORM_DATA_CONTENT_TYPE_PLAIN_IF_FILE_TYPE_UNKNOWN)) {
                contentType = MimeType.TEXT_PLAIN;
            } else {
                contentType = MimeType.APPLICATION_OCTET_STREAM;
            }
        }
        requestParameters_.add(new KeyDataPair(name, file.getFile(), fileName, contentType, (Charset) null));
    } else {
        requestParameters_.add(new NameValuePair(name, Context.toString(value)));
    }
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) Charset(java.nio.charset.Charset) File(com.gargoylesoftware.htmlunit.javascript.host.file.File) KeyDataPair(com.gargoylesoftware.htmlunit.util.KeyDataPair) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 2 with File

use of com.gargoylesoftware.htmlunit.javascript.host.file.File in project htmlunit by HtmlUnit.

the class FormData method set.

/**
 * Sets a new value for an existing key inside a {@code FormData} object,
 * or adds the key if it does not already exist.
 * @param name the name of the field whose data is contained in {@code value}
 * @param value the field's value
 * @param filename the filename reported to the server (optional)
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public void set(final String name, final Object value, final Object filename) {
    if (StringUtils.isEmpty(name)) {
        return;
    }
    int pos = -1;
    final Iterator<NameValuePair> iter = requestParameters_.iterator();
    int idx = 0;
    while (iter.hasNext()) {
        final NameValuePair pair = iter.next();
        if (name.equals(pair.getName())) {
            iter.remove();
            if (pos < 0) {
                pos = idx;
            }
        }
        idx++;
    }
    if (pos < 0) {
        pos = requestParameters_.size();
    }
    if (value instanceof File) {
        final File file = (File) value;
        String fileName = null;
        if (filename instanceof String) {
            fileName = (String) filename;
        }
        requestParameters_.add(pos, new KeyDataPair(name, file.getFile(), fileName, file.getType(), (Charset) null));
    } else {
        requestParameters_.add(pos, new NameValuePair(name, Context.toString(value)));
    }
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) Charset(java.nio.charset.Charset) File(com.gargoylesoftware.htmlunit.javascript.host.file.File) KeyDataPair(com.gargoylesoftware.htmlunit.util.KeyDataPair) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 File (com.gargoylesoftware.htmlunit.javascript.host.file.File)2 KeyDataPair (com.gargoylesoftware.htmlunit.util.KeyDataPair)2 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)2 Charset (java.nio.charset.Charset)2