无法使用Razor Pages上传相对较大的视频文件 - “请求有效性太大”

发布于 2025-02-13 04:04:23 字数 1589 浏览 2 评论 0 原文

我正在尝试创建一个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启动应用程序时,我得到了:

“

I'm trying to create a web app where I can upload video files to the cloud and then play them back. I am having trouble with uploading relative large video files. When I Upload a relatively small video file everything works as it should.

I have done extensive research into this but I cannot seem to get anything to work. I have recreated my problem in a small example for demonstration purposes

Here is the .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>

And here is the .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();

    }
}

As you can see I have added [RequestFormLimits(MultipartBodyLengthLimit = 104857600)] to the page and I have also created a web.config and extend the limit size there as well, but still I keep getting the same problem.

I have also increased the FormOptions in the StartUp.cs but still no luck:

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;
});

When I launch the application using IIS I get this:

RequestTooLargeError

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昨迟人 2025-02-20 04:04:23

对于.net Core 6和Razor页面,Microsoft文档中有一些提示( https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view = aspnetcore-6.0

)目标定位执行上传的页面:

// Add services to the container.
builder.Services.AddRazorPages(options => 
{
options.Conventions
    .AddPageApplicationModelConvention("/[your cshtml page name with no extension]",
        model =>
        {
            model.Filters.Add(
            new RequestFormLimitsAttribute()
            {
                // Set the limit to 256 MB
                ValueLengthLimit = 268435456,
                MultipartBodyLengthLimit = 268435456,
                MultipartHeadersLengthLimit = 268435456
            });
           // model.Filters.Add(
           //     new RequestSizeLimitAttribute(268435456));
        });
    });

在文档中添加了多ARTHEADERSLENGTEM LIMENGTHLEMTEMET,但我包括您将要的其他限制也对更改感兴趣。

在您的页面模型中,包括:

[DisableRequestSizeLimit]
[RequestFormLimits(MultipartBodyLengthLimit = 268435456)]
public class YourPageNameModel : PageModel
{ ...}

请注意,要遵循文档示例,如果要上传其他扩展名,则必须使用适当的文件签名更新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:

// Add services to the container.
builder.Services.AddRazorPages(options => 
{
options.Conventions
    .AddPageApplicationModelConvention("/[your cshtml page name with no extension]",
        model =>
        {
            model.Filters.Add(
            new RequestFormLimitsAttribute()
            {
                // Set the limit to 256 MB
                ValueLengthLimit = 268435456,
                MultipartBodyLengthLimit = 268435456,
                MultipartHeadersLengthLimit = 268435456
            });
           // model.Filters.Add(
           //     new RequestSizeLimitAttribute(268435456));
        });
    });

MultipartHeadersLengthLimit was added in the documentation, but I included other limits you would be interested in changing, too.

In your page model, include:

[DisableRequestSizeLimit]
[RequestFormLimits(MultipartBodyLengthLimit = 268435456)]
public class YourPageNameModel : PageModel
{ ...}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文