无法在ASP.NET Core 6 Razor页面中上传大文件x> 1gb

发布于 2025-02-08 13:19:09 字数 1357 浏览 1 评论 0 原文

我一直在寻找一种将大文件上传到服务器的方法。 我的表格适用于小文件x< 30mb。 这是我的形式:

    <div class="card">
     <div class="card-header">
         <center><b>Dodaj obrazek</b></center>
      </div>
<div class="card-body">
<form method="post" enctype="multipart/form-data">
 <div class="mb-3">
     <input type="file" asp-for="Gallery.Upload" class="form-control"  />
 </div>
 <button class="btn btn-success">Upload</button>
    </form>
    </div>
</div>

我正在使用iformfile,

 [NotMapped]
    public IFormFile Upload { get; set; }

我一直在尝试许多场景,例如在SiteMotodel之前或方法之前声明RequestFormlimit:

 [RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
public class SiteModel : PageModel

    [RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
    public async Task OnGetAsync()

但仍然什么都没有。

//更新 我已经将其添加到我的program.cs

    builder.Services.Configure<FormOptions>(conf =>
{
    conf.ValueLengthLimit = int.MaxValue;
    conf.MultipartBodyLengthLimit = int.MaxValue;
    conf.MemoryBufferThreshold = int.MaxValue;
});

和sitemotel之前

    [DisableRequestSizeLimit]

,现在我只能上传高达100 MB,但是我想使其更大,例如每个上传的4GB限制。.

如何更改限制尺寸?

i have been looking for a way to upload a large files to my server.
My form works for small files x<30MB.
This is my form:

    <div class="card">
     <div class="card-header">
         <center><b>Dodaj obrazek</b></center>
      </div>
<div class="card-body">
<form method="post" enctype="multipart/form-data">
 <div class="mb-3">
     <input type="file" asp-for="Gallery.Upload" class="form-control"  />
 </div>
 <button class="btn btn-success">Upload</button>
    </form>
    </div>
</div>

I am using IFormFile

 [NotMapped]
    public IFormFile Upload { get; set; }

I have been trying many scenarios like declaring RequestFormLimit before SiteModel or before Method:

 [RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
public class SiteModel : PageModel

    [RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
    public async Task OnGetAsync()

but still got nothing.

//UPDATE
I have added this to my Program.cs

    builder.Services.Configure<FormOptions>(conf =>
{
    conf.ValueLengthLimit = int.MaxValue;
    conf.MultipartBodyLengthLimit = int.MaxValue;
    conf.MemoryBufferThreshold = int.MaxValue;
});

and this before SiteModel

    [DisableRequestSizeLimit]

and now i can upload only up to 100 MB, but i want to make it larger, like 4GB limit per one upload..

How can i change limit size?

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

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

发布评论

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

评论(3

黑色毁心梦 2025-02-15 13:19:09

将旧应用更新为.net6后,我们遇到了同一问题,还尝试了您尝试过的所有事项。直到web.config被添加回该项目,允许大型上传成功。这是所有需要的:

<?xml version="1.0" encoding="utf-8"?> <configuration>   
<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483648" />
        </requestFiltering>
    </security>                   
</system.webServer>

希望它有帮助。

After updating an older app to .net6 we ran into the same issue, and also tried all the things you have tried. It wasn't until a web.config was added back to the project that allowed the large upload to succeed. Here is all that was required:

<?xml version="1.0" encoding="utf-8"?> <configuration>   
<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483648" />
        </requestFiltering>
    </security>                   
</system.webServer>

Hope it helps.

燕归巢 2025-02-15 13:19:09

for.net Core 6和Razor页面,我们在Microsoft文档中有一些技巧( https://learn.microsoft.com/en-us/aspnet/core/core/mvc/models/models/file-uploads?view = appnetcore-6.0

) CS,它们将文件限制作为目标的选项,以执行上传的页面为目标:

// 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,但是我包括了您也有兴趣更改的其他限制。

在您的页面模型中包括:

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

请注意,要遵循他们的示例,如果您试图上传其他扩展名,则必须使用适当的文件签名更新FileHelper.cs。否则,该页面将返回无效状态。

For.Net Core 6 and Razor Pages, we have some tips in Microsoft documentation (https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0)

In Program.cs, they included the file limits 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));
        });
});

In the documentation, they just added MultipartHeadersLengthLimit, 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 their 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.

檐上三寸雪 2025-02-15 13:19:09

这是一个安全 +优化功能。默认情况下,您每个有效载荷可以移动多少内容(数量和质量)有限制。 for .net核心根据

根据您要托管的方式,您可以更改此限制以适合您的案件。

您可以将Web.config文件添加到项目中,并添加代码。

    <system.webServer>
      <security>
        <requestFiltering>
          <requestLimits maxAllowedContentLength="209715200" />
        </requestFiltering>
      </security>
    </system.webServer>

您也可以按照(这是针对MVC的,但功能良好)。

那足够吗?否。由于TCP/IP&amp;网络结构的IEEE标准,如果您没有开放管道,那么在没有良好网络的情况下,传输大文件可能是一件琐事。它很容易破裂。

我建议您也尝试处理压缩文件。 this 教程将文件压缩到ZIP中。

 using(var compress = new GZipStream(outputFile, 
        CompressionMode.Compress, false)) 
 {
     byte[] b = new byte[inFile.Length];
     int read = inFile.Read(b, 0, b.Length);
     while (read > 0) 
     {
        compress.Write(b, 0, read);
        read = inFile.Read(b, 0, b.Length);
     }
 }.

但是,如果您正在处理视频,我会建议不要压缩ZIP或RAR。这是因为视频已经非常压缩了。但是,您可以“ traspose&amp; transform”,这意味着您可以从AVI到MKV掩盖。有时,这个过程可能会损失数据和质量。

This is a security + optimization feature. By default there is a limit on just how much content(quantity and quality) you can move per Payload. For .net core according to this article, it is about 25MB.

Depending on how you want to host, you could change this limit to suit your case.

You could add a web.config file to the project and add the code bellow.

    <system.webServer>
      <security>
        <requestFiltering>
          <requestLimits maxAllowedContentLength="209715200" />
        </requestFiltering>
      </security>
    </system.webServer>

you could also follow the microsoft documentation(This is for MVC, but it serves well).

Will that be enough? No. Due to the TCP/IP & IEEE Standard on Network structure, if you do not have an open pipeline, transfering large files can be a chore without good network. it can easily break.

I would advice you to also try working on compressed files. This tutorial compresses file into a Zip.

 using(var compress = new GZipStream(outputFile, 
        CompressionMode.Compress, false)) 
 {
     byte[] b = new byte[inFile.Length];
     int read = inFile.Read(b, 0, b.Length);
     while (read > 0) 
     {
        compress.Write(b, 0, read);
        read = inFile.Read(b, 0, b.Length);
     }
 }.

If you are dealing with Videos, however, i will advice against compressing to Zip or Rar. this is because videos are already very much compressed. you could however "traspose & transform", meaning that you could covert from an Avi to an Mkv. Sometimes, this process could me loss in data and quality.

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