gpt4 book ai didi

ruby-on-rails - 使用 Rails API 响应 native 文件上传

转载 作者:行者123 更新时间:2023-12-04 03:41:57 25 4
gpt4 key购买 nike

我正在使用 Rails 的 React Native 作为 API 构建一个移动应用程序,我在上传图像时遇到问题我使用 react-native-image-picker 库在 IOS 中上传相机胶卷图像,并使用 XMLHttRequest 连接到 API 端点为了在 Rails 中上传文件,我使用了默认的载波设置。

当我尝试从 postman 上传时,它正在工作,但从 React Native 上传时却没有。下面是我在响应 native 上传请求中的代码:

ImagePicker.launchImageLibrary(options, (response) => {
if (response.didCancel) {
console.log('User cancelled photo picker');
}
else if (response.error) {
console.log('ImagePicker Error: ', response.error);
}
else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
}
else {
var source;

// You can display the image using either:
//source = {uri: 'data:image/jpeg;base64,' + response.data, isStatic: true};

//Or:
if (Platform.OS === 'android') {
source = {uri: response.uri, isStatic: true};
} else {
source = {uri: response.uri.replace('file://', ''), isStatic: true};
}
var photo = {
uri: source.uri,
type: 'image/jpeg',
name: response.fileName
}
var xhr = new XMLHttpRequest();

var body = new FormData();
body.append('authToken', token);
body.append('avatar', photo);

xhr.open('POST', uri);

xhr.send(body)

最佳答案

我解决了我的问题,我使用 fetch 而不是 XMLHttpRequest 和

关于ruby-on-rails - 使用 Rails API 响应 native 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41510159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com