Android Multipart HTTP Post does not send the File's MIME type

后端 未结 6 1153
挽巷
挽巷 2021-02-05 21:53

Trying to figure what\'s wrong with my codings. I followed a blog post from here.

I managed to get the codes to actually upload the file to a PHP web service. However, f

相关标签:
6条回答
  • 2021-02-05 21:53

    So while all the above answers did help in getting it right.

    But just removing the HttpMultipartMode.BROWSER_COMPATIBLE wouldn't solve it.

    You can set the mime type by doing this

    ByteArrayBody bab = new ByteArrayBody(params.getByteArray("data"), "image/jpeg" , "image.jpg");
    

    N.B. I am using the latest repackaged android client http://code.google.com/p/httpclientandroidlib/ which supports multipart uploads, etc.

    0 讨论(0)
  • 2021-02-05 21:54

    We have the same issue here. Using HttpMultipartMode.BROWSER_COMPATIBLE removed the correct mimetype being sent.

    Unfortunately using the 'normal' MultipartEntity() is not an option for us, as this way the rails backend messes up the request handling.

    Is there any way to set the content-type of the multipart yourself and use HttpMultipartMode.BROWSER_COMPATIBLE??

    0 讨论(0)
  • 2021-02-05 21:55

    I just had the same issue when updating HttpClient from version 4.1 to version 4.4. Neither removing HttpMultipartMode.BROWSER_COMPATIBLE nor replacing with other alternative options did help.

    A solution that requires the minimum change for me is to explicitly specify the filename when constructing a FileBody, i.e. replacing

    new FileBody(file, mimeType)
    

    with

    new FileBody(file, filename, mimeType, charset)
    

    As for version HttpClient 4.4, the reason might be that new FileBody(file, mimeType) assigns null to the filename field rather than uses file.getName(). Then, when executing formatMultipartHeader in HttpMultipart, the filename of the part that is going to be formatted is checked if not null in BROWSER_COMPATIBLE mode. If null, then the whole body is ignored.

    0 讨论(0)
  • 2021-02-05 21:55

    I had the same problem for awhile for posting 3 FileBodies and 1 mime info, the mime (in json) was never received unless I sent only one filebody. I always addpart the filebody first which caused me the problem. I reversed the order by addpart the mime at very beginning and every info including mime are received correctly at php server end.

    0 讨论(0)
  • 2021-02-05 21:59

    I had the same problem and just fixed it.

    I found that using the HttpMultipartMode.BROWSER_COMPATIBLE prevented the correct mimeType from being set in my request, when using a ByteArrayBody for an image. I assume this is probably the same problem you are having.

    When I changed this line:

    MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    

    to

    MultipartEntity mp = new MultipartEntity();
    

    then the mime type was set correctly and the service upload worked.

    I see that people used the BROWSER_COMPATIBLE for solving another problem, but hopefully you don't need it.

    0 讨论(0)
  • 2021-02-05 21:59

    Instead of including the jars files you can use the http multipart classes defined in android but declared as internal, so you cannot use them directly, you have to download them from the android source code and add them to your project. Some basics modifications must be performed before using them, nothing difficult (change the packages name, remove all logger calls). It works perfectly for me.I hope this could help somebody,

    0 讨论(0)
提交回复
热议问题