I am trying to upload an image to Django backend server with React Native.
JavaScript
x
16
16
1
const addPostHandle = () => {
2
const formData = new FormData()
3
formData.append("image", {
4
name: "img",
5
type: image.mime,
6
size: image.size,
7
uri: Platform.OS === "android" ? image.path : image.path.replace("file://", "")
8
})
9
formData.append("title", title)
10
formData.append("category", category)
11
addPost(formData)
12
13
setTitle('')
14
setCategory(0)
15
}
16
but I got an error that says File Extension "" is not allowed. Allowed extension are...
and there is no problem with the data I sent.
JavaScript
1
2
1
[["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]]
2
After some research I found out that a lot of people faced with this problem when using react-native-image-crop-picker
but they get network error
however I don’t have a problem with sending the data. So I don’t think this problem caused by Flipper
P.S: this is the backend code if needed:
JavaScript
1
10
10
1
class PostCreateAPIView(generics.CreateAPIView):
2
queryset = Post.objects.all()
3
serializer_class = PostCreateSerializer
4
permission_classes = [IsAuthenticated | IsAdminUser]
5
parser_classes = (MultiPartParser,) #FormParser
6
7
def perform_create(self, serializer):
8
print(self.request.__dict__)
9
serializer.save(author=self.request.user)
10
Advertisement
Answer
After thousands of tries, I found out that changing name: "img"
to name: "img.jpg
fixed the issue.