我正在尝试使用React Native将图像上传到Django后端服务器。
const addPostHandle = () => {
const formData = new FormData()
formData.append("image", {
name: "img",
type: image.mime,
size: image.size,
uri: Platform.OS === "android" ? image.path : image.path.replace("file://", "")
})
formData.append("title", title)
formData.append("category", category)
addPost(formData)
setTitle('')
setCategory(0)
}
但是我收到一个错误消息File Extension "" is not allowed. Allowed extension are...
,并且我发送的数据没有问题。
[["image", {"name": "img", "size": 63410, "type": "image/jpeg", "uri": "file:///storage/emulated/0/Android/data/com.mobile/files/Pictures/2defe993-c6c4-44e4-8438-c0d57b5bd16f.jpg"}], ["title", "cat-test"], ["category", 5]]
经过一些研究,我发现很多人在使用react-native-image-crop-picker
时都会遇到此问题,但是他们得到了network error
,但是我在发送数据方面没有问题。因此,我认为此问题不是由Flipper
P.S:如果需要,这是后端代码:
class PostCreateAPIView(generics.CreateAPIView):
queryset = Post.objects.all()
serializer_class = PostCreateSerializer
permission_classes = [IsAuthenticated | IsAdminUser]
parser_classes = (MultiPartParser,) #FormParser
def perform_create(self, serializer):
print(self.request.__dict__)
serializer.save(author=self.request.user)
答案 0 :(得分:0)
经过数千次尝试,我发现将name: "img"
更改为name: "img.jpg
可以解决此问题。