我有一个MVC项目,该视图从一个视图上传一个或多个文件到服务器, 然后,当我单击“提交”时,将撤回一个证明的Web服务,并传递一组文件进行上传。 上载工作正常,但从webapi表单中,我有一个我要返回的对象(类Store)。
我不知道从网络api返回“原始”对象是错误还是读取错误。
这是我的模特
public class Store
{
public IDictionary<string, string> dictionaryOfStores = new Dictionary<string, string>();
public string CodeText { get; set; }
}
这是我的网络api方法:
public async Task<ActionResult<Store>> UploadAsync()
{
dictStores = new Store();
foreach (var formFile in form.Files)
{
dictStores.dictionaryOfStores.Add(formFile.FileName, codefilename);
// also CodeText is filled here
}
}
}
// return dictionary
//
return dictStores;
}
这是我的MVC控制器,可调用私有Web服务
public async Task<IActionResult> Upload(List<IFormFile> files)
{
using (var client = new HttpClient())
{
var model = new FilesViewModel();
MultipartFormDataContent multiContent = new MultipartFormDataContent();
foreach (var formfile in files)
{
client.BaseAddress = new Uri(BaseAddress);
byte[] data;
using (var br = new BinaryReader(formfile.OpenReadStream()))
data = br.ReadBytes((int)formfile.OpenReadStream().Length);
ByteArrayContent bytes = new ByteArrayContent(data);
multiContent.Add(bytes, "file", formfile.FileName);
model.Files.Add(new FileDetails { Name = formfile.GetFilename() });
}
var result = await client.PostAsync("api/Load/Upload", multiContent); //
// here i have try to read result.Content but doesn't have my data
var res = await result.Content.ReadAsStreamAsync();
// here i have to put res in my model
return View("Summary", model);
}
我使用ASP Net Core 2.2
感谢您的帮助
答案 0 :(得分:0)
也许是这样
IDictionary<MyClass, int> resource = new Dictionary<MyClass, int>
{
{ new Class {Property1="1"}, 1 },
{ new Class {Property1="11"}, 1 }
};
return Ok(resource);
答案 1 :(得分:0)
我通过看到答案来解决:How to get object using Httpclient with response Ok in Web Api 我能够读取数据:
await result.Content.ReadAsAsync<Store>()