简体   繁体   English

如何读取附加为 formData.append 的 blob 内容

[英]How to read blob content appended as formData.append

I would like to be able to read and pass the innerHtml content of an entire div including all the sub-elements server-side using PHP.我希望能够使用 PHP 读取和传递整个 div 的 innerHtml 内容,包括服务器端的所有子元素。 I have successfully managed to upload it as as a blob with我已经成功地将它作为一个 blob 上传

 function scriptSubmit() { var fd = new FormData(); var container = document.querySelector('.textbody').innerHTML; //get the div with text elements var blob = new Blob([container], { type: "text/html" }); fd.append('blob', blob, ); var xhr = new XMLHttpRequest(); xhr.open("POST", "scripts.php"); xhr.onreadystatechange = function(e) { if (xhr.readyState == 4 && xhr.status == 200) { console.log(xhr.status); } } xhr.send(fd); }
 <div class="textbody"> <div><span>sample text 1</span></div> <div><span>sample text 2</span></div> <div><span>sample text 3</span></div> <div>More sample text</div> </div> <button type="button" onclick="scriptSubmit()"> Submit <button>

(and getting server response == 200 on XMLHttpRequest) but i am having trouble reading the blob as plain text. (并在 XMLHttpRequest 上获得服务器响应 == 200)但我无法将 blob 作为纯文本读取。 My reason for doing this is I would like to be able to use the text as html input that I can parse server-side with another script or if possible within the same script.我这样做的原因是我希望能够将文本用作 html 输入,我可以使用另一个脚本或如果可能的话在同一个脚本中解析服务器端。 In short I would like to be guided on the server-side operations Thank you总之希望能指导一下关于服务器端的操作 谢谢

Remove the last ',' int fd.append('blob', blob, );删除最后一个 ',' int fd.append('blob', blob, ); . .

Check your variable $_POST server side with var_dump.使用 var_dump 检查您的变量 $_POST 服务器端。

For help u, u can create a div like <div id="output"></div> and print the server response by changing this if为了帮助你,你可以创建一个像<div id="output"></div>这样的 div 并通过改变这个来打印服务器响应 if

if (xhr.readyState == 4 && xhr.status == 200) {
   console.log(xhr.status);
}

into this if如果进入这个

if (xhr.readyState == 4 && xhr.status == 200) {
   console.log(xhr.status);
   document.getElementById('output').innerHTML = xhr.responseText;
}

EDIT:编辑:

Data not in the $_POST but in the $_FILES数据不在 $_POST 但在 $_FILES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM