如何获取表单数据的大小?

时间:2017-10-21 14:31:06

标签: javascript fetch-api

如何在JavaScript中获取FormData的大小?
我找到similar question,但在答案中,仅测量值的大小。

我想使用fetch API发送FormData。所以,我需要FormData的整体大小为Content-Length。

提前致谢!

EDIT1

我想发送FormData,如下所示:

let myhead = new Headers();
let formdata = new FormData();
formdata.append("Name", "AAABBB");
// add several data... like picture, video, and so on.
myhead.append("content-type", "application/x-www-form-urlencoded");
myhead.append("content-type", "multipart/form-data"); // I don't know which should I use.
myhead.append("content-length", "the value of size of formdata"); # 

fetch("http://localhost:8744/post", {
    method: "POST",
    mode: "cors",
    body: formdata,
    headers: myhead
});

EDIT2

我尝试使用改进的代码发送到FormData,如下所示但是浏览器继续加载太多时间。

改进版本:

let myheaders = new Headers();
myheaders.append("connection", "close")
let formdata = new FormData();
formdata.append("Hello", "World");

fetch("http://localhost:6788/nk", {
    method: "POST",
    mode: "cors",
    body: formdata,
    headers: myheaders
})

0 个答案:

没有答案