JavaScript-How does a form submit a picture in base64 format?

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.