c# - 如何上传文件mvc6?

标签 c# asp.net-core-mvc

我想要实现上传 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);
    }

最佳答案

这是我的解决方案:

在客户端 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);
        }

关于c# - 如何上传文件mvc6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37854091/

相关文章:

c# - 如何在自定义验证属性中获取/注入(inject)服务

c# - 按数组值排序数组索引

c# - CRM 2011 使用 ICodeWriterFilterService 生成的代码无法构建

asp.net-core-mvc - 如何将 'jump' 从 MVC ViewComponent 转移到另一个 Controller ?

c# - 跨多个项目共享 wwwroot

c# - 从 C# .NET 调用使用 C 样式数组作为参数的 C 方法

c# - Google chrome 在后续调用中丢失 MVC Auth Cookie(Set-Cookie 指令)

asp.net - UseIdentityServerBearerTokenAuthentication 不适用于 IdentityServer3

asp.net-core - aspnet-codegenerator : No code generators available,即使添加了Microsoft.VisualStudio.Web.CodeGeneration.Design

c# - ListBox SelectionMode 单选多选