我想要实现上传压缩文件的api。
我的函数用于文本文件,而不是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);
}发布于 2017-03-03 13:02:32
这是我的解决方案:
论客户端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);
}https://stackoverflow.com/questions/37854091
复制相似问题