簡體   English   中英

使用改造(pdf、ppt、png、jpg)“代碼= 500,內部服務器錯誤”上傳文件的問題是什么

[英]what is the issue in file uploading using retrofit (pdf,ppt, png,jpg) "code=500, internal server error"

在此處輸入圖像描述我正在使用改造設置文件上傳功能,但出現內部服務器錯誤。 有時文件正在上傳到服務器上,但有詳細信息,但響應出現在錯誤正文中。 我不知道我的代碼或服務器端代碼有什么問題。

我為請求正文嘗試了不同的代碼。

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("pic", file.getName(), requestFile);

  File file = new File(PathHolder);

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

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

     RequestBody userId = 
     RequestBody.create(MediaType.parse("text/plain"), uid);

     RequestBody topic = RequestBody.create(MediaType.parse("text/plain"), 
     topicName);

     RequestBody classID = 
     RequestBody.create(MediaType.parse("text/plain"), classId);

     RequestBody shiftID = 
     RequestBody.create(MediaType.parse("text/plain"), shiftId);

     Call<UploadDocumentResponse> resultCall = service.uploadImage(userId, 
        topic, classID, shiftID, body);

     resultCall.enqueue(new Callback<UploadDocumentResponse>() {

     @Override

     public void onResponse(Call<UploadDocumentResponse> call, 
        Response<UploadDocumentResponse> response) {

      progressDialog.dismiss();

      onBackPressed();

       UploadDocumentResponse iresponse = response.body();

      // ViewDocModel Success or Fail
      if (response.isSuccessful()) {
      Toast.makeText(getApplicationContext(), response.body().
      getMsg(), Toast.LENGTH_LONG).show();

      imageView.setImageDrawable(null);

      imagePath = null;
     }

   }

     @Override
     public void onFailure(Call<UploadDocumentResponse> call, Throwable t) 
      {

        progressDialog.dismiss();

         Toast.makeText(getApplicationContext(), "couldn't upload file, 
         please try 
         again", Toast.LENGTH_LONG).show();

        }
    });

   // APIInterface

   @Multipart
   @POST("Camp/API_uploadfile.php")
   Call<UploadDocumentResponse> uploadImage(@Part("uid") RequestBody uid,
                                         @Part("topic") RequestBody 
                                         topic,
                                         @Part("classid") RequestBody 
                                                              classId,
                                         @Part("shiftid") RequestBody 
                                                              shiftId,
                                         @Part MultipartBody.Part file);

預期結果是文件應該上傳到服務器並成功獲得響應。 實際結果是收到內部服務器錯誤,有時在錯誤正文中收到成功響應消息。

我已經解決了這個問題。 我使用了錯誤的請求正文模式。 我不得不使用多部分表單數據而不是文本。

File file1 = new File(imageFilePathFront);
        RequestBody requesFile = RequestBody.create(MediaType.parse("multipart.form- 
data"), file1);
        cnicFront = MultipartBody.Part.createFormData("CNIC_FRONT_IMG", 
file1.getName(), requesFile);

這是對我有用的正確代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM