【发布时间】:2023-03-12 21:03:02
【问题描述】:
我正在制作一个表单,用于下载用户放入其中的数据。到目前为止,文件下载但不是用户的输入,而是返回[object Object]
我尝试使用 JSON.Stringify() 但它返回的文件中包含“未定义”。
即使 console.log() 给了我{username: "asdasd", password: "sdasdasd"}
e.preventDefault();
console.log(formData);
var formDataString = JSON.stringify(FormData);
// ... submit to API or something
download(formDataString, 'json.txt', 'text/plain');
};
const initialFormData = Object.freeze({
username: "",
password: "",
});
function download(formDataString, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([formDataString], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
我的完整代码可见here
提前感谢您的帮助。
【问题讨论】:
标签: javascript json reactjs forms form-data