[英]Extract Base64 string from an object
I am sending a base64 string
to my node.js
server. 我正在将
base64 string
发送到我的node.js
服务器。 But its adding the string inside object brackets {}
但是它在对象括号
{}
添加了字符串
Front-end code: 前端代码:
I encode
the base64 string to preserve it. 我
encode
base64字符串进行encode
以保留它。
let options = {
headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
};
let encodedImage = encodeURIComponent(body);
return this._http.put<IUser>(`${BASE_URL}/api/users/${id}/photo`, encodedImage, options)
.map(data => {
return data;
})
.catch(err => {
// do whatever you want when error occurs
console.log(err);
// re-throw error so you can catch it when subscribing, fallback to generic error code
return Observable.throw(err || 'API_ERROR');
});
} }
Serverside: 服务器端:
let upload = (req, res) => {
let b64string = req.body;
console.log(b64string);
}
The console log is: 控制台日志为:
{ 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQA.....+idkVe3H/9k=': '' }
How can I extract the base64
string from within the object so I can access it. 如何从对象中提取
base64
字符串,以便可以访问它。 it seems like a simple thing but my brain has given up. 看起来很简单,但是我的大脑已经放弃了。 Any help will be greatly appreciated.
任何帮助将不胜感激。
Update: 更新:
Update 2: 更新2:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
There seems to be something putting your string in an JSON encoded value, hence the {'...' : '...'}. 似乎有些东西将您的字符串放入JSON编码的值中,因此{'...':'...'}。 You could put the base64 encoded string in an json object from the get go, that way you have an expected input when you get the object.
您可以从一开始就将base64编码的字符串放在json对象中,这样,在获取对象时便有了预期的输入。 You just need to get the string from the json key.
您只需要从json键获取字符串即可。
Hope this helps. 希望这可以帮助。
The problem here is that you creating a PUT
request with 这里的问题是您使用以下命令创建了一个
PUT
请求
Content-Type : application/x-www-form-urlencoded
but you pass a single string as your data. Content-Type : application/x-www-form-urlencoded
但是您传递了一个字符串作为数据。 So the body-parser
sees this string as a key
with an empty value
and that's why you get this console log. 因此,
body-parser
将此字符串视为具有空value
的key
,这就是为什么要获得此控制台日志的原因。
So the solution to this, 因此,解决方案
let options = {
headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
};
let requestBody = new URLSearchParams()
requestBody.append('encodedImage',body)
return this._http.put < IUser > (`${BASE_URL}/api/users/${id}/photo`, requestBody, options)
So we said we have a content type of form-urlencoded
(that means all of your data will be passed as giant url query) and we passed a urlencoded form (through the URLSearchParams) 因此,我们说过,我们有一种内容
form-urlencoded
的内容(这意味着您的所有数据都将作为巨型url查询传递),并且我们传递了一个urlencoded形式(通过URLSearchParams)
Please bear in mind URLSearchParams browser compatibility. 请记住URLSearchParams浏览器的兼容性。
Please consider using multipart/form-data
for your file uploads instead, if you don't have any limitations. 如果没有任何限制,请考虑改用
multipart/form-data
进行文件上传。 The logic of the above will be the same except the new URLSearchParams()
will be new FormData()
. 除了
new URLSearchParams()
将是new FormData()
之外,上述逻辑将相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.