React Native 图片上传文件扩展名错误

React Native Image Upload File Extension Error

我正在尝试使用 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)

经过数千次尝试,我发现将 name: "img" 更改为 name: "img.jpg 可以解决问题。