无法使用Razor Pages上传相对较大的视频文件 - “请求有效性太大”
我正在尝试创建一个Web应用程序,可以将视频文件上传到云,然后将其播放。我在上传相对大型视频文件时遇到困难。当我上传一个相对较小的视频文件时,一切都可以正常工作。
我已经对此进行了广泛的研究,但我似乎无法实现任何工作。我已经在一个示例中重新创建了我的问题,以进行演示目的
是.cshtml:
<form method="post" enctype="multipart/form-data">
<input type="file" asp-for="UploadedFile" />
<input asp-page-handler="Upload" class="btn" type="submit" value="Upload">
</form>
您可以看到,这是.cshtml.cs,
[RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
public class TrialUploadModel : PageModel
{
[BindProperty]
public IFormFile UploadedFile { get; set; }
public void OnGet()
{
}
public async Task<IActionResult> OnPostAsync()
{
Console.Write("Success!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
using (var stream = UploadedFile.OpenReadStream())
{
Console.Write(stream.Length);
}
return Page();
}
}
我已经添加了 [requestFormLimits(MultipartBodyLempentLimit = 104857600)] Page和我也创建了一个Web.config并在此处扩展了限制大小,但是我仍然会遇到同样的问题。
我还增加了启动的formoptions.cs,但仍然没有运气:
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
x.MultipartHeadersLengthLimit = int.MaxValue;
});
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = int.MaxValue;
});
当我使用IIS启动应用程序时,我得到了:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于.net Core 6和Razor页面,Microsoft文档中有一些提示( https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view = aspnetcore-6.0
)目标定位执行上传的页面:
在文档中添加了多ARTHEADERSLENGTEM LIMENGTHLEMTEMET,但我包括您将要的其他限制也对更改感兴趣。
在您的页面模型中,包括:
请注意,要遵循文档示例,如果要上传其他扩展名,则必须使用适当的文件签名更新fileHelper.cs。否则,该页面将返回无效状态。
For .NET Core 6 and Razor Pages, there are some tips in Microsoft documentation (https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0)
In Program.cs, file limits are included as an option targeting the page that performs the upload:
MultipartHeadersLengthLimit was added in the documentation, but I included other limits you would be interested in changing, too.
In your page model, include:
Note that to follow the documentation example, you must update the FileHelper.cs with the proper file signature if you are trying to upload a different extension. Otherwise, the page will return an invalid state.