Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6553715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:36:54+00:00 2026-05-25T12:36:54+00:00

I am trying to to send a base64 encoded img to server,the javascript looks

  • 0

I am trying to to send a base64 encoded img to server,the javascript looks like

var xhr=new XMLHttpRequest()
var reader=new FileReader()
reader.onloadend=function(e){
xhr.onload=function(e){
alert(xhr.responseText)
}
xhr.open("POST","upload.php");
        xhr.setRequestHeader("Cache-Control", "no-cache");
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        //xhr.setRequestHeader("X-File-Name", file.name);
        //xhr.setRequestHeader("X-File-Type",file.type)
xhr.send(e.target.result)
}
reader.readAsDataURL(file)
},false)

In php,looks like this:

echo "some response Text";
$postdata = file_get_contents("php://input");
file_put_contents('MyFile.jpg', base64_decode($postdata));

And,eventually,the server gets a file exactly as big as the sent file,However,it can’t be opened
Some one get some ideas?Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:36:55+00:00Added an answer on May 25, 2026 at 12:36 pm
    reader.readAsDataURL(file)
    

    A data URL is NOT the same as a base64 version of the file. You get extra garbage in it. It looks like this:

    data:[<MIME-type>][;charset=<encoding>][;base64],<data>
    

    See Wikipedia.

    Try doing a simple regex on it:

    var data = dataURL.match(/,(.*)$/)[1];
    

    Or, in your code,

    xhr.send(e.target.result.match(/,(.*)$/)[1]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://static.reddit.com/reddit.com.header.png', true); xhr.responseType = 'arraybuffer'; xhr.onload = function(e)
I'm trying to send a base64 encoded data using a POST request and it
While trying to send a POST request via xmlhttp.open(POST, url, true) (javascript) to the
im trying to send a unix command from a client to a server, wait
I´m trying to send a image through tcp to a server, firts getting the
I am trying to send Email using telnet and smtp.mail.yahoo.com server. when I try
I'm trying to authenticate with an XMPP server using SASL. /** * Send Authentication,
I'm trying to send data to a server. That is, some string fields and
I'm trying to send-retrive message from server by SOAP... but soapConnection.call( soapMessage, endpoint )
Trying to send some email in my C# app. I am behind a proxy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.