繁体   English   中英

使用Retrofit2上传文件后,获取“磁盘上的0字节”文件

[英]Getting a file with “0 bytes on disk” after uploading it using retrofit2

我使用的是我作为学校项目制作的网站的移动版本,该网站本身运行正常,我的后端似乎也正常运行,所以我使用的是相同的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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM