android上如何上传文件?formdata 不存在

想用xmlhttprequest formdata 来上传文件。
在web上可以成功。
android上就不行
提示formdata not defined.

难道还要另写一套代码用java去上传?

提供给下demo,我们验证下,应该是可以的才对。

确实不行
我已经改了实现方式 。
按照form-data的格式自己把所有请求体组装为bytearray来上传 成功了。

代码大概这样。

`
//Binary 是 string|ArrayBuffer|Blob|Uint8Array

private async post(xhr: XMLHttpRequest) {
    let boundary = `Boundary_${Math.floor(Math.random() * 10000000)}`
    xhr.setRequestHeader("Content-Type", `multipart/form-data; boundary=${boundary}`)
    let datas: Binary[] = []
    for (let v of this.req.blobs) {
        let type = v.type || "application/octstream"
        let filename = v.filename || "upload"
        datas.push(`--${boundary}\r\n`)
        datas.push(`Content-Disposition: form-data; name="${v.key}"; filename="${filename}"\r\n`)
        datas.push(`Content-Type: ${type}\r\n`)
        datas.push("\r\n")
        datas.push(v.data)
        datas.push("\r\n")
    }
    datas.push(`--${boundary}--`)

    if (cc.sys.isBrowser) {
        xhr.send(new Blob(datas))
    } else {
        let bytes: Array<number>[] = []
        for (let v of datas) {
            if (typeof v == "string") {
                bytes.push(sputils.string2utf8(v))
            } else if (v instanceof ArrayBuffer) {
                bytes.push(sputils.arraybuffer2array(v))
            } else if (v instanceof Uint8Array) {
                let array: number[] = []
                for (let i = 0; i < v.length; i++) {
                    array.push(v[i])
                }
                bytes.push(array)
            }
        }
        let data: number[] = []
        for (let v of bytes) {
            for (let n of v) {
                data.push(n)
            }
        }
        let ab: ArrayBuffer = sputils.array2arraybuffer(data)
        xhr.send(ab)
    }
}

`

另外原生平台好像也不支持FileReader.
方法调用没有报错,但是onloaded等一些回调方法一直收不到回调。

ios 提示ERROR: ReferenceError: Can’t find variable: FormData, location: src/project.dev.js:19611:32

你好 我也碰到了同样的问题 在ios平台 let formData = new FormData(); 报错 信息如下
ERROR: ReferenceError: Can’t find variable: FormData, location: src/project.js:64661:21
STACK:
request@src/project.js:64661:21
src/project.js:62632:24
src/project.js:587:73
invoke@src/cocos2d-jsb.js:28927:33
global code@(no filename):1:13

你好 请问后来是怎么解决的?

解决了吗?怎么沉了?

no multipart boundary param in Content-Type,您好,我们在使用multipart/form-data做post的时候出现了这个错误,但加了boundary后又不知道怎么组织FormData数据,请大佬指点下

sputils 能分享一下吗,报错了

UploadImageTest.zip (1.3 MB)

export class Form {

//有 FormData

private formData: FormData;

//无 FormData

private boundary: Uint8Array;

public contentType: string;

private data: Array<string | Uint8Array>;

public constructor() {

    if (window.FormData) {

        this.formData = new FormData();

    } else {

        const boundary = "------------------------------7m" + (0x100000000000 + Math.floor(Math.random() * 0x100000000000)).toString(16).substr(1);

        this.contentType = "multipart/form-data; boundary=" + boundary;

        this.boundary = this.cs2u8s("--" + boundary);

        this.data = [];

    }

}

private cs2u8s(cs: string): Uint8Array {

    let i: number = cs.length;

    const u8s = new Uint8Array(i);

    while (i--) {

        u8s[i] = cs.charCodeAt(i);

    }

    return u8s;

}

private str2u8s(str: string): Uint8Array {

    return this.cs2u8s(unescape(encodeURIComponent(str)));

}

public append(key: string, value: Uint8Array | string | number, fileName?: string): void {

    if (this.formData) {

        if (fileName) {

            this.formData.append(key, new Blob([value as Uint8Array], { type: "application/octet-binary" }), fileName);

        } else {

            this.formData.append(key, value + "");

        }

    } else {

        this.data.push(this.boundary, '\r\nContent-Disposition: form-data; name="', this.str2u8s(key), '"');

        if (fileName) {

            this.data.push('; filename="', this.str2u8s(fileName), '"\r\nContent-Type: ', contentTypes.get(value as Uint8Array), "\r\n\r\n", value as Uint8Array, "\r\n");

        } else {

            this.data.push('\r\n\r\n', this.str2u8s(value + ""), "\r\n");

        }

    }

}

public getBody(): FormData | Uint8Array {

    if (this.formData) {

        return this.formData;

    } else {

        const u8ss: Array<Uint8Array> = [];

        for (const part of this.data) {

            u8ss.push(part instanceof Uint8Array ? part : this.cs2u8s(part));

        }

        u8ss.push(this.boundary, this.cs2u8s("--\r\n"));

        let L: number = 0;

        for (const u8s of u8ss) {

            L += u8s.length;

        }

        const bytes = new Uint8Array(L);

        let i: number = 0;

        for (const u8s of u8ss) {

            for (const byte of u8s as any) {

                bytes[i++] = byte;

            }

        }

        u8ss.length = 0;

        return bytes;

    }

}

}

手搓

这是接收端代码:receive.php.zip (1.2 KB)