The backend is the PHP $_files variable to get the submitted file.
The normal form submission is this way.
正常表单提交,enctype=multipart/form-data
My situation here is that the uploaded file is a string of base64 from outside, how to change this, in order to achieve the simulation form submission, the background does not need to change the code.
Ps:
I try to put Base64 in the Formdata object, but the $_files variable in the background is still not recognized
function sendFormByBase64(){ var form = document.getElementById('form-2'); var formData = new FormData(); var base64 ='/* 这里面是base64字符串 */'; formData.append('file', base64); var xhr = new XMLHttpRequest(); xhr.open('POST', form.action, true); xhr.send(formData);}
PS2: Attaching the Grab-bag diagram again
Base64 's Grab Bag
Normal post Submission Form file capture
Reply content:
The backend is the PHP $_files variable to get the submitted file.
The normal form submission is this way.
正常表单提交,enctype=multipart/form-data
My situation here is that the uploaded file is a string of base64 from outside, how to change this, in order to achieve the simulation form submission, the background does not need to change the code.
Ps:
I try to put Base64 in the Formdata object, but the $_files variable in the background is still not recognized
function sendFormByBase64(){ var form = document.getElementById('form-2'); var formData = new FormData(); var base64 ='/* 这里面是base64字符串 */'; formData.append('file', base64); var xhr = new XMLHttpRequest(); xhr.open('POST', form.action, true); xhr.send(formData);}
PS2: Attaching the Grab-bag diagram again
Base64 's Grab Bag
Normal post Submission Form file capture
I just met you this question, but I use tornado+ajax, hope to help you
Base64 image data, you directly with the normal form ( xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded ");
), as a string after the server can be, but because some special characters of the form data is very dangerous, so URL encodingencodeURIComponent()
function sendimg(b64img){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { // } else { // } } }; xhr.open("post", "/upload.php", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded "); xhr.send("img="+encodeURIComponent(shared.imageDataURL));}
The data that the server accepts to the form is the name
img
URL encoded base64 data, which is what you want after decoding.
About Ajax and background processing code can look at my small project:)
Ps: Wood has to look at the time, the main question in the first half of the Qaq, now the problem should have been solved ~ o (∩∩) o ...