reactjs - 如何创建用户上传头像功能
问题描述
我在我的项目中使用 nextjs 和 cloudinary-react:
这是我的上传功能代码库:
const uploadImage = () => {
const formData = new FormData();
formData.append("file", imageSelected);
formData.append("upload_preset", "teammateme");
axios.post("https://api.cloudinary.com/v1_1/teammateme/image/upload", formData)
.then((response) => {
console.log(response);
});
};
return (
<Card className="profile--card_container">
<CardContent>
{picture ? (
<div>
<Avatar
src={picture}
alt="Avatar"
className="avatar--profile_image"
/>
<input
type="file"
onChange={(event) => {
setImageSelected(event.target.files[0]);
}}
/>
<button onClick={uploadImage}>Submit</button>
</div>
) : (
<AccountCircleIcon className="avatar--profile_image" />
)}
)
如何为用户头像制作上传图片?
解决方案
如果我理解正确,您正在尝试secure_url
在成功上传到 Cloudinary 后获得返回。为此,您可以替换:
axios.post("https://api.cloudinary.com/v1_1/teammateme/image/upload", formData)
.then((response) => {
console.log(response);
});
和:
axios.post("https://api.cloudinary.com/v1_1/teammateme/image/upload", formData)
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
var url = data.secure_url;
});
这将记录完整的 Cloudinary 响应,然后secure_url
专门提取url
变量中的响应。
推荐阅读
- c# - ASP NET 是否有另一种方法可以在不使用 RedirectToAction 的情况下将值从控制器中的当前函数复制到另一个控制器?
- react-native - 如何在堆栈导航中制作透明背景或切换标题
- nim-lang - Nim 中的 try 块未捕获该错误
- excel - 使用 IRibbonControl 的时间执行 VBA SUB-SUB
- c# - 如何在 C# 中取消/中止 TCP IP 请求
- embedded - 示例代码中的中断条件在哪里
- python - 用于 FPGrowth 的 Pyspark 数据帧格式 -> 输入列必须是数组,但得到 bigint
- .net - 如何在剃刀页面中获取对象
- reactjs - 如何在 React Redux 中按大小过滤产品
- java - 折叠任一以返回不同的值