如何设置动态文件名? [英] How to set dynamic filename?

查看:80
本文介绍了如何设置动态文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用改造 2,我如何为上传的文件设置动态名称?

Using retrofit 2, how would I set a dynamic name for the uploaded file?

目前是这样的:

@Part("avatar\"; filename=\"image\" ") RequestBody image,

但是,上传的文件名将是 image 没有扩展名.关于这个案例有什么建议吗?

However, the uploaded file name would be image without the extension. Any recommendation on this case?

推荐答案

使用 MultipartBody.Part 作为类型定义你的端点:

Define your endpoint with MultipartBody.Part as the type:

interface Example {
  @Multipart //
  @POST("/foo/bar/") //
  Call<ResponseBody> method(@Part MultipartBody.Part part);
}

然后使用它的工厂来创建类型:

and then use its factories to create the type:

RequestBody body = // image body...
Call<ResponseBody> call = example.method(
    MultipartBody.Part.createFormData("image", "whatever.png", body));

这篇关于如何设置动态文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆