【发布时间】:2021-02-20 07:48:08
【问题描述】:
我正在尝试将我的 react 本机应用程序中的图像上传到 nodejs 和 cloudinary 云,但它不适合我。那是我的 react-native 代码:
const App = () => {
const [profileImageSource ,setProfileImageSource] = useState(require('./images/profilePic.png'));
const [imageData, setImageData] = useState("");
const [okay, setOkay] = useState(false);
const config = {
withCredentials: true,
baseURL: 'http://localhost:3000/',
headers: {
"Content-Type": "application/json",
},
};
const handleProfileImage = () => {
const options = {
title: 'Select photo',
base64: true
};
ImagePicker.showImagePicker(options, (res) => {
if(res.didCancel){
}
else if(res.error){
}
else{
const image = {data: res.data};
axios
.post('/clients/upload-image', {
data: JSON.stringify({image})
},
config
)
.then((res) => console.log("ok"))
.catch((err) => alert(err));
}
})
}
这是我的nodejs代码:
require('dotenv').config();
const cloudinary = require('cloudinary').v2;
//Cloudinary configurations
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});
app.post('/clients/upload-image', (req, res) => {
const imageStr = req.body.data;
const uploadedResponse =
cloudinary
.uploader
.upload(imageStr, {
upload_preset: 'user',
})
.then((uploadedResponse) => console.log(uploadedResponse))
.catch((err) => console.log("error"));
}
我哪里错了?我不断收到错误,图片没有上传到云端
【问题讨论】:
标签: node.js react-native cloudinary react-native-image-picker