Ruby on rails 使用Rails API响应本机文件上载
我正在使用react-native with Rails作为API构建一个移动应用程序,我在上载图像时遇到了一个问题,我使用react-native图像选择器库在IOS和XMLHttRequest中上载相机滚动图像,用于连接到API端点,并在Rails中上载文件,我使用了默认的carrierwave设置 当我尝试从邮递员上传时,它正在工作,但从react native上传时却没有。以下是我在react native upload request中的代码:Ruby on rails 使用Rails API响应本机文件上载,ruby-on-rails,api,react-native,Ruby On Rails,Api,React Native,我正在使用react-native with Rails作为API构建一个移动应用程序,我在上载图像时遇到了一个问题,我使用react-native图像选择器库在IOS和XMLHttRequest中上载相机滚动图像,用于连接到API端点,并在Rails中上载文件,我使用了默认的carrierwave设置 当我尝试从邮递员上传时,它正在工作,但从react native上传时却没有。以下是我在react native upload request中的代码: ImagePicker.launchIm
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,并且我忘记了头中的授权