2016-07-27 98 views
0

我想上傳圖片到使用改造2發送圖像文件與改造2

的圖像通過圖像採摘活動回升,似乎因爲文件(影像)的工作我們項目的服務器可以使用顯示畢加索。

改裝成功,但服務器似乎沒有得到該文件。 這是服務器端的一部分。

func (c *gin.Context) { 
    file, header , err := c.Request.FormFile("profileImage") 
    // err = http: no such file 
} 

Sever side error message

即使RequestBody打印時我測試相干信息(尺寸,圖像類型......)

服務:

@Multipart 
@PATCH("/user/profileImage") 
Call<ResponseBody> modifyUserImage(@Part("profileImage") RequestBody profileImage, @Part("userID") RequestBody userID); 

以下代碼是部分相同片段類

開幕圖像採摘活動:

Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT); 
getIntent.setType("image/*"); 

Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
pickIntent.setType("image/*"); 

Intent chooserIntent = Intent.createChooser(getIntent, "Select Image"); 
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent}); 

startActivityForResult(chooserIntent, 1); 

在活動結果:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == Activity.RESULT_OK && requestCode == 1) { 
     // process the result 
     Uri selectedImage = data.getData(); 
     String wholeID = DocumentsContract.getDocumentId(selectedImage); 
     String id = wholeID.split(":")[1]; 
     String[] column = {MediaStore.Images.Media.DATA}; 
     String sel = MediaStore.Images.Media._ID + "=?"; 
     Cursor cursor = getActivity().getContentResolver(). 
       query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
         column, sel, new String[]{id}, null); 
     String filePath = ""; 
     int columnIndex = cursor.getColumnIndex(column[0]); 
     if (cursor.moveToFirst()) { 
      filePath = cursor.getString(columnIndex); 
     } 
     cursor.close(); 

     file = new File(filePath); 
     Picasso 
       .with(getActivity().getApplicationContext()) 
       .load(file) 
       .into(civ_userProfilePicture); 
    } 
} 

請求:

Call<ResponseBody> call = ServiceSingelton.getmInstance().getService() 
         .modifyUserImage(RequestBody.create(MediaType.parse("image/*"), file), 
             RequestBody.create(MediaType.parse("text/plain"), ServiceSingelton.getmInstance().getUserID())); 

call.enqueue(new Callback<ResponseBody>() { 
    @Override 
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
     if (response.code() == 200) { 
      Log.d("RETROFIT SUCCESS", "Pic should be sent"); 
     } else { 
      Log.d("RETROFIT SUCCESS", "Error code received modifying user"); 
     } 
    } 

    @Override 
    public void onFailure(Call<ResponseBody> call, Throwable t) { 
     Log.d("RETROFIT ERROR", t.getMessage()); 
    } 
}); 
+0

我們可以看到一些您的服務器端處理代碼嗎? – snkashis

+0

使用服務器端代碼進行編輯 – Vark

回答

1

有人給了我這個修復作用後:

要發佈與文件名的一部分,你應該改變@part( 「profileImage」) RequestBody profileImage到@part RequestBody profileImage,並通過 它MultipartBody.Part.createFormData(零件名稱,文件名,requestBody):

// Service 
@Multipart 
@PATCH("/user/profileImage") 
Call<ResponseBody> modifyUserImage(@Part MultipartBody.Part profileImage, @Part("userID") RequestBody userID); 

// Call 
MultipartBody.Part imagePart = MultipartBody.Part.createFormData("profileImage", file.getName(), RequestBody.create(MediaType.parse("image/*"), file)); 
Call<ResponseBody> call = ServiceSingelton.getmInstance().getService() 
         .modifyUserImage(imagePart, 
             RequestBody.create(MediaType.parse("text/plain"), ServiceSingelton.getmInstance().getUserID()));