我想要实现上传zip文件的api。
我的功能适用于文本文件但不适用于zip文件。 Zip文件已保存但无法打开。 你知道这对此很好吗?
在客户端,我会在下一步行动中致电api:
[HttpPost]
public async Task<IActionResult> Upload(ICollection<IFormFile> files)
{
using (var client = new HttpClient())
{
foreach (var file in files)
{
if (file.Length > 0)
{
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var fileContent = new StreamContent(file.OpenReadStream());
fileContent.Headers.Add("X-FileName", fileName);
fileContent.Headers.Add("X-ContentType", file.ContentType);
var response = await client.PostAsync(url2, fileContent);
}
}
}
return View(nameof(this.Index));
}
这是我的api:
[HttpPost]
public async Task<IActionResult> Post()
{
var input = new StreamReader(Request.Body).ReadToEnd();
var fileName = Request.Headers["X-FileName"];
var fileType = Request.Headers["X-ContentType"];
using (var sw = new StreamWriter(@"C:\" + fileName))
{
sw.Write(input);
}
await Task.FromResult(0);
return new ObjectResult(true);
}
答案 0 :(得分:0)
这是我的解决方案:
在客户端API上
[HttpPost("{lastModified}")]
public async Task<string> Upload(long lastModified)
{
using (var client = new HttpClient())
{
foreach (var file in Request.Form.Files)
{
if (file.Length > 0)
{
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var fileContent = new StreamContent(file.OpenReadStream());
var archiveUrl = "path to api with 2 parameters {fileName}/{lastModified}";
var datasetResponse = await client.PostAsync(archiveUrl, fileContent);
var dataset = await datasetResponse.Content.ReadAsStringAsync();
return dataset;
}
}
throw new ApplicationException("Cannot updated dataset to archive");
}
}
在服务器API上
[HttpPost("{fileName}/{lastModified}")]
public async Task<IActionResult> Post(string fileName, long lastModified)
{
var dataSet = getDataSet();
return new ObjectResult(dataSet);
}