我正在编码以将多个图像从android上传到服务器,并且一个图像可以正确上传,而多个图像则不能。
我需要一些有关代码的帮助。服务器使用c#.net核心,Android使用Retrofit + rxjava。
服务器端(C#.net核心)
[HttpPost]
public async Task<IActionResult> UploadFile(IFormFile file)
{
BaseEntity<string> be = new BaseEntity<string>();
if (file == null || file.Length == 0)
{
be.code = "1";
be.message = "no file";
be.data = "";
return Ok(JsonConvert.SerializeObject(be));
}
// change file name with its extension
var fileName = Guid.NewGuid().ToString() +
System.IO.Path.GetExtension(file.FileName);
var path = Path.Combine(
Directory.GetCurrentDirectory(), "wwwroot/userImgs",
fileName);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
be.code = "0";
be.message = "success";
be.data = fileName.ToString();
return Ok(be);
}
[HttpPost]
public async Task<IActionResult> UploadFiles(List<IFormFile> files)
{
BaseEntity<List<string>> be = new BaseEntity<List<string>>();
if (files == null || files.Count == 0)
{
be.code = "1";
be.message = "files shows no content";
be.data = null;
return Ok(JsonConvert.SerializeObject(be));
}
List<string> paths = new List<string>();
foreach (var file in files)
{
if (file.Length > 0)
{
var path = Path.Combine(
Directory.GetCurrentDirectory(), "wwwroot/userImgs",
file.FileName);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
paths.Add(file.FileName);
}
}
be.code = "1";
be.message = "success";
be.data = paths;
return Ok(JsonConvert.SerializeObject(be));
}
Android方面:
Web Apis:
@Multipart
@POST("Upload/UploadFile")
Observable<BaseEntity<String>> UploadFile(@Part MultipartBody.Part file);
@Multipart
@POST("Upload/UploadFiles")
Observable<BaseEntity<String>> UploadFiles(@Part() List<MultipartBody.Part> files);
致电:
ArrayList<MultipartBody.Part> files=new ArrayList<>();
if(list.get(p).localImgURLs.size()!=0)
{
for (String url:list.get(p).localImgURLs
) {
File file = new File(url);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
files.add(body);
}
}
Api.getInstance().UploadFiles(files)
.subscribeOn(Schedulers.io())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@io.reactivex.annotations.NonNull Disposable disposable) throws Exception {
((ExecCheckActivity)mContext).startWait();
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<BaseEntity<String>>() {
@Override
public void accept(@io.reactivex.annotations.NonNull BaseEntity<String> be) throws Exception {
((ExecCheckActivity)mContext).stopWait();
MyApplication.setResultToToast(be.getMessage());
}
}, new Consumer<Throwable>() {
@Override
public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
((ExecCheckActivity)mContext).stopWait();
ExceptionHelper.handleException(throwable);
}
});
问题是,当我调用Api.getInstance()。UploadFile(files.get(0))时,它将起作用,而当我调用Api.getInstance()。UploadFiles(files)时,它将无法正常工作。
答案 0 :(得分:0)
您的服务必须接收文件数组,只需修改您的后端代码
public async Task<IActionResult> UploadFile(IFormFile[] file)
答案 1 :(得分:0)
我知道了。
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
应更改为:
MultipartBody.Part body = MultipartBody.Part.createFormData("files", file.getName(), requestFile);