【问题标题】:Uploading large files with WebApi使用 WebApi 上传大文件
【发布时间】:2017-03-01 20:56:33
【问题描述】:

我正在使用 ngResource 和 Angular 将文件上传到 ASP.NET Web 服务。

在我尝试超过一定大小(约 50MB)的文件之前,一切正常。在某个大小之后,FromBody 参数没有被绑定,我得到了一个null DocumentDTO 对象引用。

有什么解决办法吗?

在我的 web.config 中,我有以下设置:

maxAllowedContentLength="500000000"

maxRequestLength="200000"

在我的控制器中,我有:

[HttpPost, Route("{id:Guid}")]
public async Task<IHttpActionResult> Insert([FromUri]Guid id, [FromBody]DocumentDTO documentDTO)
{
    //...

我的DocumentDTO 是:

public class DocumentDTO
{
    [Required]
    public Guid Id { get; set; }

    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [MaxLength(250)]
    public string FileName { get; set; }

    [Required]
    public string Content { get; set; }

    [MaxLength(250)]
    public string ContentType { get; set; }

}

在我的 Angular 控制器中,我有以下代码:

vm.document.$save(
            data => {

                notifications.success("The document has been uploaded to the repository.", "Upload Succeeded");

            },
            err => {

                errorService.handleApiError(err, "document", "upload");

            })
            .finally(() => vm.loading = false);

通过在FileReader 上调用readAsArrayBuffer 来设置vm.document.content 值。

就像我说的:这一切在一定的文件大小下都可以正常工作。是FileReader有限制没有正确设置文件内容,还是WebApi对模型绑定有限制?

【问题讨论】:

  • 尝试使用 Postman 或其他 HTTP 客户端测试文件上传到服务器。

标签: c# asp.net asp.net-web-api


【解决方案1】:

请同时更新 maxRequestLength。以下是 2GB 文件的示例。

<system.web>
  <httpRuntime maxRequestLength="2097152" />
</system.web>

【讨论】:

  • 我不确定你是否读到我正在设置 maxRequestLength="200000"。我的文件大小将低于 150MB,所以应该足够了。
  • 啊,是的,我错过了阅读……尝试在这里复制。另一种选择是吐出和合并文件 [link]codeproject.com/Articles/1034347/…
猜你喜欢
  • 2013-06-23
  • 2015-08-16
  • 2017-07-05
  • 1970-01-01
  • 2015-12-06
  • 2017-09-06
  • 2019-02-16
  • 2018-09-05
相关资源
最近更新 更多