如何使用HTTP POST发送图像为base64字符串中的JSON? [英] How to send image as base64 string in JSON using HTTP POST?

查看:873
本文介绍了如何使用HTTP POST发送图像为base64字符串中的JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从上传用户电脑中的图片。首先,我读的形象作为二进制字符串,然后连接code将其用base64,然后张贴作为一个JSON对象的一部分。不过,我得到一个HTTP 500错误,我不知道为什么。我曾尝试用硬codeD短字符串替换reader.result,它的工作(我有一个HTTP 200)。所以我不知道,如果字符串长度(这是一个图像二进制字符串很长)是这里的主要因素。从其中进行发送和接收的POST的领域是不同的如果该事项。可能是什么错在这里?先谢谢了。

错误消息:


  

[2014-01-11 14点23分58秒]信息的WEBrick :: HTTPServer定位#启动:PID = 77263
  端口= 9292
  127.0.0.1 - - [11/1月/ 2014年14点24分37秒]POST / pull_request HTTP / 1.1500 185316 0.0413


客户端code:

 如果(图片&安培;&安培; image.type.match(IMAGETYPE)){
    变种文件名=时间戳+' - '+ image.name;
    VAR读卡器=新的FileReader();
    reader.onload =功能(E){
        写(图像/+文件名,Base64.en code(reader.result)的base64');
    };
    reader.readAsBinaryString(图片);
}函数write(路径,内容编码){
    VAR请求= {请求:{路径:路径,内容:内容,编码:编码}};
    $阿贾克斯({
        键入:POST,
        网址:HTTP://本地主机:9292 /上传
        数据:JSON.stringify(要求)
    });}

服务器端code。我删除了JSON解析和因为此刻我只想得到一个HTTP 200上传简单的东西,每当我收到一个POST,而不管数据的内容:

 要求辛纳特拉
需要.to_json要求JSON /分机#
需要'github_api做配置
    用户名= ENV ['GITHUB_USER'] || '用户名'
    密码= ENV ['GITHUB_PASSWORD'] || '密码'
    github_login =%S:%s的%[用户名,密码]
    GitHub的= Github.new basic_auth:github_login
    集:GitHub上,GitHub上
    设置:USER_NAME,someusername
    设置:回购,'somerepo
结束邮报/请求吗
    状态200
结束


解决方案

你的JavaScript code似乎是正确的。
当您收到HTTP 500错误(500内部服务器错误),您的错误应该在服务器端code。请检查您的服务器端code或分享在这里。

I'm trying to upload an image from user's computer. First I read the image as a binary string, then encode it in base64 and then POST it as part of a JSON object. However, I get a HTTP 500 error and I have no idea why. I have tried replacing reader.result with a hardcoded short string and it worked (I got a HTTP 200). So I'm not sure if the string length (which is very long for an image binary string) is the major factor here. The domain from which the POST is being sent and received are different if that matters. What could be wrong here? Thanks in advance.

Error message:

[2014-01-11 14:23:58] INFO WEBrick::HTTPServer#start: pid=77263 port=9292 127.0.0.1 - - [11/Jan/2014 14:24:37] "POST /pull_request HTTP/1.1" 500 185316 0.0413

Client side code:

if(image && image.type.match(imageType)) {
    var fileName = timestamp+'-'+image.name;
    var reader = new FileReader();
    reader.onload = function(e) {
        write('images/'+fileName, Base64.encode(reader.result), 'base64');
    };
    reader.readAsBinaryString(image);
}

function write(path, content, encoding){
    var request = {"request":{"path": path, "content": content, "encoding": encoding}};
    $.ajax({
        type: "POST",
        url: "http://localhost:9292/upload",
        data: JSON.stringify(request)
    });

}

Server side code. I removed the JSON parsing and upload stuff for simplicity because at the moment I just want to get a HTTP 200 whenever I receive a POST, regardless of data content:

require 'sinatra'
require 'json/ext' # required for .to_json
require 'github_api'

configure do
    username = ENV['GITHUB_USER'] || 'username'
    password = ENV['GITHUB_PASSWORD'] || 'password'
    github_login = "%s:%s"  % [username, password]
    github = Github.new basic_auth: github_login
    set :github, github
    set :user_name, 'someusername'
    set :repo, 'somerepo'
end

post "/request" do
    status 200
end

解决方案

your javascript code seems right. As you are getting http 500 error (500 Internal Server Error) your error should be in server side code. Please check your server side code or share here.

这篇关于如何使用HTTP POST发送图像为base64字符串中的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆