如何在django服务器端处理多部分体图像上传?

时间:2017-08-06 19:19:30

标签: android python django-rest-framework retrofit image-uploading

我已经实现了使用@MultipartBody将图像上传到服务器的改进客户端实现,但无法在服务器端处理上传:

这是我的客户端实现(只是为了确保我做得对):

/** 
 * Activity
 */
@OnClick(R.id.change_profile_image_button)
public void addProfileImage() {
    Intent pickIntent = new Intent();
    pickIntent.setType("image/*");
    pickIntent.setAction(Intent.ACTION_GET_CONTENT);
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    String pickTitle = "Take or select a photo";
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takePhotoIntent });
    startActivityForResult(chooserIntent, REQUEST_CODE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (data.getData() == null) {
            Toast.makeText(getActivity(),
                    "Image file could not be uploaded",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        mProfileImageFile = getFileFromUri(data.getData());
        mProfilePresenter.uploadProfileImage(mProfileImageFile);
    }
}

private File getFileFromUri(Uri fileUri) {
    if (fileUri != null) {
        String filePath = fileUri.getPath();
        if (filePath != null) {
            return new File(filePath);
        }
    }
    return null;
}

/** 
 * UserRepository
 */

@Override
public void uploadProfileImage(File file) {
    RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);
    RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "upload");

    Call<ResponseBody> req = userServiceApi.uploadProfileImage(body, name);
    req.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(@NonNull Call<ResponseBody> call,
                               @NonNull Response<ResponseBody> response) {
            // Do Something
            if (response.isSuccessful()) {
                //String location = response.body();
                //eventBus.post(new SuccessUploadProfileImageEvent(location));
                Log.d(TAG, "Successfully uploaded image");
            } else {
                eventBus.post(new FailUploadProfileImageEvent());
            }
        }

        @Override
        public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
            t.printStackTrace();
        }
    });
}

/**
 * UserService
 */
@Multipart
@POST("users/upload-profile-image/")
Call<ResponseBody> uploadProfileImage(@Part MultipartBody.Part image,
                                      @Part("name") RequestBody name);

在服务器端,我尝试实现此视图,但它不起作用:

class UserProfileUploadImageView(RetrieveModelMixin, UpdateModelMixin, generics.GenericAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = (IsAuthenticated,)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    @detail_route(methods=['POST'], permission_classes=[IsAuthenticated])
    @parser_classes((FormParser, MultiPartParser,))
    def image(self, request, *args, **kwargs):
        if 'upload' in request.data:
            user_profile = self.get_object()
            user_profile.image.delete()

            upload = request.data['upload']

            user_profile.image.save(upload.name, upload)

            return Response(status=HTTP_201_CREATED, body={'url': user_profile.image.url})
        else:
            return Response(status=HTTP_400_BAD_REQUEST)

在用户模型中:

profile_image = models.ImageField(blank=True, null=True, upload_to="profile_image")

0 个答案:

没有答案