无法使用改造2上传图像

时间:2018-01-30 10:26:26

标签: php android rest retrofit2

我正在尝试使用post方法向用户注册图像,用户已注册但图像未上传且没有错误,当我发送大图像时响应时间增加,所以你猜你的图像已被发送。

我看到很多类似的问题,但没有得到解决方案。

从Chrome扩展程序成功上传的图片支持api请求和HTML表单。

改装请求

@Multipart
@POST("system/miscellaneous/api?")
Call<String> register(
        @Part("params[mobno]") String username,
        @Part("params[password]") String pass,
        @Part("params[company]") String company,
        @Part("params[email]") String email,
        @Part("params[contactname]") String contactname,
        @Part("params[phonenumber]") String phonenumber,
        @Part MultipartBody.Part logo,
        @Part("function") String fun

);

从图库中获取图片

realImagePathFromURI = getRealPathFromURI(uri);

File file = new File(realImagePathFromURI);

  if (file.exists()){
    Log.d(TAG, "onActivityResult: file exist");
  }


requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);


body = MultipartBody.Part.createFormData("name", file.getName(), requestFile);

电话

Call<String> call = service.register(
                        username,
                        pass,
                        companyName,
                        email,
                        contactName,
                        contactNum,
                        body,
                        "empregister");

服务器上的注册功能

public static function empregister($params)
{
    $emp_mobileno      = ApiPlugin::array_get($params, "mobno");
    $emp_password      = ApiPlugin::array_get($params, "password");
    $emp_companyname   = ApiPlugin::array_get($params, "company");
    $emp_email         = ApiPlugin::array_get($params, "email");
    $emp_contactname   = ApiPlugin::array_get($params, "contactname");
    $emp_phonenumber   = ApiPlugin::array_get($params, "phonenumber");
    $emp_logo          = basename($_FILES['logo']['name']);

    echo "Image uploaded is: " .$emp_logo;

    $check_user_exists = SJB_DB::query("SELECT * FROM users where username='$emp_mobileno'");
    $user_exists       = SJB_DB::getAffectedRows();
    $data              = array();
    $fill              = explode('.', $emp_logo);
    $fill[0]           = addslashes(str_replace("'", "", $fill[0]));
    $ext               = pathinfo($emp_logo, PATHINFO_EXTENSION);
    $filerenamed       = $fill[0] . "_thumb";
    $filext            = $ext;
    if ($filext == 'png' || $filext == 'jpg' || $filext == 'jpeg') {
        $mime = 'application/' . $filext;
    }
    $savedfilename = $filerenamed . $filext;
    if ($user_exists == "") 
    {
        $activation_date      = date("Y-m-d H:i:s");
        $password             = md5($emp_password);
        $insert_query         = "INSERT INTO  users SET username='$emp_mobileno',password='$password',email='$emp_email',user_group_sid=41,registration_date='$activation_date',PhoneNumber='$emp_phonenumber',CompanyName='$emp_companyname',ContactName='$emp_contactname',logo='$emp_logo',active=1,approval='Approved',sendmail=0,language='en',featured=0,ip='' ";
        $insert_query_execute = SJB_DB::query($insert_query);

        if (isset($emp_logo)) 
        {
            $insert_upload_table = mysql_query("INSERT INTO uploaded_files(id,file_name,file_group,saved_file_name,storage_method,mime_type) VALUES ('$Resume_Id','$filerenamed','pictures','$savedfilename','file_system','$mime')");
        }
        $path          = "/var/wwwdev/files/files/";
        $target_file   = $path . basename($_FILES["logo"]["name"]);
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
        if (($imageFileType == "png") || ($imageFileType == "jpg") || ($imageFileType == "jpeg")) {
            $result = move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file);
        } 
        else {//echo "<p>Logo must be in jpg or png format</p>";}
        $msg = "User registered";
        return array(
            "results=" => "User registered"
        );
    } else {
        $msg = "Mobile number already exists, please try to login";
        return array(
            "results=" => $msg
        );
    }
}

1 个答案:

答案 0 :(得分:1)

花了1.5天后,我发现了问题。

MultipartBody.Part.createFormData方法中的第一个字符串paramamer在改进调用请求方法中需要与@Part MultipartBody.Part logo参数EQUALS

所以我改变了

body = MultipartBody.Part.createFormData("name", file.getName(), requestFile);

 body = MultipartBody.Part.createFormData("logo", file.getName(), requestFile);

我试图找到有关此参数的信息,但文档中没有解释,请小心!