将文件添加到条带连接
Adding files to stripe connect
我正在尝试将一些文件(身份证明文件)添加到 stripe 以创建关联帐户,但我无法将它们从客户端上传到 stripe。我的后端在 Node.js 中,Stripe 文档说它应该使用这种格式:
const Stripe = require('stripe');
const stripe = Stripe('stripeAPIKEY');
var fp = fs.readFileSync('/path/to/a/file.jpg');
var file = await stripe.files.create({
purpose: 'identity_document',
file: {
data: fp,
name: 'file.jpg',
type: 'image/jpg',
},
});
我需要上传文件数据(变量fp),但在Javascript中,我似乎无法获得用户在客户端上传文档时的相关路径。这是我对 Stripe 的函数调用:
export const uploadPersonIdFile = async (identityDocument: any) => {
const fp = fs.readFileSync(identityDocument);
const personId = await stripe.files.create({
purpose: 'identity_document',
file: {
data: fp,
name: 'idDocument.jpg',
type: 'image/jpg',
},
});
return personId;
}
我的客户端是这样的:
const inpFileU = $("#utilityButton");
const previewImage = $("#image-preview__image-U");
const previewDefaultText = $("#image-preview__default-text-U");
inpFileU.change(function(){
const file = this.files[0];
if(file){
const reader = new FileReader();
previewImage.css("display", "block");
reader.addEventListener("load", function(){
previewImage.attr("src", this.result);
});
reader.readAsDataURL(file);
utilityFileName = file.name;
await uploadMerchantUtilityDocument({
utilityDocument: utilityFileName
}).then((result) => {
/** @type {any} */
const data = result.data;
const textData = data.text;
console.log(JSON.stringify(data));
console.log(JSON.stringify(textData));
}).catch((error) => {
console.log("Error message: " + error.message);
console.log("Error details: " + error.details);
});
} else {
console.log('no file');
}
});
我上传了文件,然后我不断收到的错误消息响应是这样的:
错误:ENOENT:没有那个文件或目录,打开 'insuranceImage.jpeg'
我应该如何上传我的文件?我认为我的 fp 变量是错误的,但我不知道用什么替换它
根据错误和评论,您提供给 readFileSync
的路径似乎没有指向您尝试读取的文件所在的位置。从错误看来,您正在传递 insuranceImage.jpeg
并且系统找不到该文件。
请尝试确认您提供的相对路径是否正确,或者构建并提供绝对路径。
据我了解,您正在将文件从客户端上传到服务器,并从要上传到条带的服务器 API。在这种情况下,当您读取带有 fs
的文件时,默认编码为 utf8
,但可能在上传文件时将其编码为 base64。我对jquery了解不多。检查它是如何编码的,所以使用正确的编码。
const image= fs.readFileSync('/path/to/file.jpg', {encoding: 'base64'});
我觉得不知何故图像被破坏了。如果图片路径正确,只需手动将图片放在该目录下,然后从中读取为utf8
编码。
因为你得到这个错误base64 string is too large to process in fs.readFileSync
这意味着你的路径是正确的。
reader = fs.createReadStream('imagePath', {
flag: 'a+',
// then try this to base64
encoding: 'UTF-8',
start: 5,
end: 64,
highWaterMark: 16
});
// Read and display the file data on console
reader.on('data', function (chunk) {
console.log(chunk);
});
好的,所以过了一段时间我得到了在线高手的帮助。这是他所做的(因为 stripe 的文档在文件上传方面不太好)...
我从 FileReader() 创建了一个 base64 变量,它来自于创建一个数组 base64String = []
这个数组获取了 reader.load 的结果,像这样:
reader.addEventListener("load", function(){
base64String.push(this.result);
});
此 base64String 数组随后用作后端函数的 identityDocument 变量。
所以我在 client-side 中的 base64 变量继续以 'image/jpeg; base64,' 开头,我需要摆脱它。使用 fs.readFileSync 没有意义,因为我们没有将文件上传到 stripe,我们正在上传原始 base64 数据。因此,node.js 中的以下代码非常适合解决此问题:
const parts = utilityDocument.split(",");
const base64 = parts[1];
const buffer = Buffer.from(base64, "base64");
所以调用stripe的完整函数是这样的:
export const uploadPersonIdFile = async (uid: any, identityDocument: any) => {
const parts = identityDocument.split(",");
const base64 = parts[1];
const buffer = Buffer.from(base64, "base64");
const personId = await stripe.files.create({
purpose: 'identity_document',
file: {
data: buffer,
name: 'identity.jpg',
type: 'image/jpg',
},
});
await updateMerchantId(uid, { idDocument: personId.id });
return personId;
}
我正在尝试将一些文件(身份证明文件)添加到 stripe 以创建关联帐户,但我无法将它们从客户端上传到 stripe。我的后端在 Node.js 中,Stripe 文档说它应该使用这种格式:
const Stripe = require('stripe');
const stripe = Stripe('stripeAPIKEY');
var fp = fs.readFileSync('/path/to/a/file.jpg');
var file = await stripe.files.create({
purpose: 'identity_document',
file: {
data: fp,
name: 'file.jpg',
type: 'image/jpg',
},
});
我需要上传文件数据(变量fp),但在Javascript中,我似乎无法获得用户在客户端上传文档时的相关路径。这是我对 Stripe 的函数调用:
export const uploadPersonIdFile = async (identityDocument: any) => {
const fp = fs.readFileSync(identityDocument);
const personId = await stripe.files.create({
purpose: 'identity_document',
file: {
data: fp,
name: 'idDocument.jpg',
type: 'image/jpg',
},
});
return personId;
}
我的客户端是这样的:
const inpFileU = $("#utilityButton");
const previewImage = $("#image-preview__image-U");
const previewDefaultText = $("#image-preview__default-text-U");
inpFileU.change(function(){
const file = this.files[0];
if(file){
const reader = new FileReader();
previewImage.css("display", "block");
reader.addEventListener("load", function(){
previewImage.attr("src", this.result);
});
reader.readAsDataURL(file);
utilityFileName = file.name;
await uploadMerchantUtilityDocument({
utilityDocument: utilityFileName
}).then((result) => {
/** @type {any} */
const data = result.data;
const textData = data.text;
console.log(JSON.stringify(data));
console.log(JSON.stringify(textData));
}).catch((error) => {
console.log("Error message: " + error.message);
console.log("Error details: " + error.details);
});
} else {
console.log('no file');
}
});
我上传了文件,然后我不断收到的错误消息响应是这样的: 错误:ENOENT:没有那个文件或目录,打开 'insuranceImage.jpeg'
我应该如何上传我的文件?我认为我的 fp 变量是错误的,但我不知道用什么替换它
根据错误和评论,您提供给 readFileSync
的路径似乎没有指向您尝试读取的文件所在的位置。从错误看来,您正在传递 insuranceImage.jpeg
并且系统找不到该文件。
请尝试确认您提供的相对路径是否正确,或者构建并提供绝对路径。
据我了解,您正在将文件从客户端上传到服务器,并从要上传到条带的服务器 API。在这种情况下,当您读取带有 fs
的文件时,默认编码为 utf8
,但可能在上传文件时将其编码为 base64。我对jquery了解不多。检查它是如何编码的,所以使用正确的编码。
const image= fs.readFileSync('/path/to/file.jpg', {encoding: 'base64'});
我觉得不知何故图像被破坏了。如果图片路径正确,只需手动将图片放在该目录下,然后从中读取为utf8
编码。
因为你得到这个错误
base64 string is too large to process in fs.readFileSync
这意味着你的路径是正确的。reader = fs.createReadStream('imagePath', { flag: 'a+', // then try this to base64 encoding: 'UTF-8', start: 5, end: 64, highWaterMark: 16 }); // Read and display the file data on console reader.on('data', function (chunk) { console.log(chunk); });
好的,所以过了一段时间我得到了在线高手的帮助。这是他所做的(因为 stripe 的文档在文件上传方面不太好)...
我从 FileReader() 创建了一个 base64 变量,它来自于创建一个数组 base64String = []
这个数组获取了 reader.load 的结果,像这样:
reader.addEventListener("load", function(){
base64String.push(this.result);
});
此 base64String 数组随后用作后端函数的 identityDocument 变量。
所以我在 client-side 中的 base64 变量继续以 'image/jpeg; base64,' 开头,我需要摆脱它。使用 fs.readFileSync 没有意义,因为我们没有将文件上传到 stripe,我们正在上传原始 base64 数据。因此,node.js 中的以下代码非常适合解决此问题:
const parts = utilityDocument.split(",");
const base64 = parts[1];
const buffer = Buffer.from(base64, "base64");
所以调用stripe的完整函数是这样的:
export const uploadPersonIdFile = async (uid: any, identityDocument: any) => {
const parts = identityDocument.split(",");
const base64 = parts[1];
const buffer = Buffer.from(base64, "base64");
const personId = await stripe.files.create({
purpose: 'identity_document',
file: {
data: buffer,
name: 'identity.jpg',
type: 'image/jpg',
},
});
await updateMerchantId(uid, { idDocument: personId.id });
return personId;
}