使用 retrofit2 上传文件后获取带有"0 bytes on disk"的文件



我正在在我作为学校项目的网站的移动版本中工作,网站本身正常工作,我的后端似乎也很好,所以我使用的是同样的PHP文件,我只是在构建前端侧。

我正在使用RetRofit2来消耗我的PHP文件,并且遇到了问题,我无法从移动应用程序中正确上传任何文件,整个数据库内容都可以正常工作,但是当涉及到上传文件时出错,上传的文件在磁盘上具有0个字节

1.-我使用意图从画廊获得图片

Intent intent = new Intent();
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Selecciona las imagenes"), PICK_IMAGE_REQUEST );

OnActivityRestult:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
            File file = new File(getPathFromURI(getContext(), data.getData()));
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), getPathFromURI(getContext(), data.getData()));
            multipartBody =MultipartBody.Part.createFormData("file",file.getName(),requestFile);
        }
    }

2.-我呼叫php文件

RetrofitInterface retrofitInterface = RetrofitClient.createRetrofit().create(RetrofitInterface.class);
// The first 8 params are for the database stuff, the last param "multipartBody" is the one who doesnt work
    Call<RespuestaGenerica> call = retrofitInterface.crearEvento(idUsuario, nombre, descripcion,
            domicilio, fecha, entrada, ciudad, result, multipartBody);
    call.enqueue(new Callback<RespuestaGenerica>() {
        @Override
        public void onResponse(Call<RespuestaGenerica> call, Response<RespuestaGenerica> response) {
            Log.d(TAG, response.body().toString());
        }
        @Override
        public void onFailure(Call<RespuestaGenerica> call, Throwable t) {
            Toast.makeText(getContext(), "Error al crear el evento: " + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

改装接口:

@Multipart
@POST("conexiones/contenido/eventos/crearEvento.php")
Call<RespuestaGenerica> crearEvento(@Part("idUsuario")String idUsuario, @Part("nombre")String nombre,
                                    @Part("descripcion")String descripcion, @Part("domicilio")String domicilio,
                                    @Part("fecha")String fecha, @Part("entrada")String entrada,
                                    @Part("ciudad")String ciudad, @Part("tags")String tags,
                                    @Part MultipartBody.Part file);

这是我存储文件的后端功能

function guardarImagenes ($idEvento) {
    $cont = 0;
    foreach($_FILES as $aux) {
        $ext = 'jpg';
        $nombreFichero = $cont++  . '.' . $ext; // e.j 0.jpg
        $ruta = '../../../media/eventos/'.$idEvento;
        if(!is_dir($ruta)) // Make dir if doesnt exists
            mkdir($ruta);
        $ruta .= '/' . $nombreFichero; // Full route
        move_uploaded_file($aux['tmp_name'], $ruta); // Attempt to upload it
    }
}

我不知道为什么要上传一个在磁盘上具有0个字节的文件,请帮助!谢谢C:

问题可能与使用的媒体类型有关,您可以尝试:

MediaType.parse(getContentResolver().getType(fileUri))

而不是让MediaType.parse("multipart/form-data"硬编码

相关内容

最新更新