【发布时间】:2017-08-12 04:18:26
【问题描述】:
我正在使用带有 Rails 作为 API 的 react native 构建移动应用程序,我在上传图像时遇到问题 我使用 react-native-image-picker 库在 IOS 中上传相机胶卷图像,使用 XMLHttRequest 连接到 API 端点在 Rails 中上传文件时,我使用了默认的载波设置。
当我尝试从邮递员上传时,它正在工作,但从本地反应却没有。以下是我在响应本机上传请求中的代码:
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)
【问题讨论】:
标签: ruby-on-rails api react-native