在asp.net核心中获取大于30MB的文件上传的404响应

问题描述 投票:-1回答:1

我在前端使用fileUploader primng,在后端使用ASP.net Core WebAPI。

对于大于30MB的文件上传,我收到404错误。

前端(.html):

<p-fileUpload
#fileUpload
name="demo[]"
customUpload="true"
showUploadButton="false"
showCancelButton="false"
chooseLabel="بارگذاری"
auto="auto"
(uploadHandler)="onUpload($event, fileUpload)"
multiple="multiple"
maxFileSize="50000000000"
>

前端(.ts)

onUpload(event,fileUpload) {
      debugger;


        for(let file of event.files) {

            this.uploadedFiles.push(file);

        }
        var downloadfile= new  DownloadFile();
        downloadfile.id = this.downloadFile.id

        if(this.downloadFile.downloadSubCategory!=null)
         this.downloadFile.downloadSubCategorylId = this.downloadFile.downloadSubCategory.id
        downloadfile.downloadSubCategorylId = this.downloadFile.downloadSubCategorylId
        downloadfile.caption = this.downloadFile.caption
        downloadfile.uploadedFiles = this.uploadedFiles

        this.fileService.uploadFile(downloadfile).subscribe(
          data => {
            console.log(data);

             data.uploadedFiles.forEach(file => {
               this.files.uploadedFiles.push(file)
             });
             console.log("TCL: TicketDetailComponent -> onUpload -> files", this.files)
             this.uploadedFiles = []
             fileUpload.clear();
          },
          error => {
            console.log(error)
          },
        );
        event.values = []

      }

后端(webapi)

[HttpPost("uploadFile")]
[DisableRequestSizeLimit]
public async Task<IActionResult> uploadFile(List<IFormFile> files, [FromForm]string id,[FromForm]string downloadSubCategoryId, [FromForm]string caption)
        {
            var uploadedFiles = new List<object>();
            DownloadFile entity;

            foreach (var file in files)
            {
                string[] str = file.FileName.Split(".");
                int length = str.Length;
                //string st = reverse(file.FileName);
                var guid = Guid.NewGuid().ToString();
                var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Files/DownloadFile", guid + "." + file.FileName.Split(".")[length-1]);
                if (Convert.ToInt32(id) > 0)
                {
                    entity = await _context.DownloadFiles.FindAsync(id);


                }
                else
                {
                    entity = new DownloadFile();

                    _context.DownloadFiles.Add(entity);

                }
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                    var contentType = file.ContentType;

                    entity.CreateDate = DateTime.Now;
                    entity.DownloadSubCategorylId = Convert.ToInt32(downloadSubCategoryId);
                    entity.MimeType = contentType;
                    entity.Caption = caption;
                    entity.FileGuidName = guid;

                    entity.FileType = file.FileName.Split(".")[length - 1];
                    entity.FileUrl = DownloadFile.GethPath(entity);


                    await _context.DownloadFiles.AddAsync(entity);
                    await _context.SaveChangesAsync(cancellationToken: default);
                    uploadedFiles.Add(new { Id = entity.Id, url = DownloadFile.GethPath(entity), entity.MimeType });
                }


            }
            return Ok(new { Message = "فایل ها با موفقیت بارگذاری شد", uploadedFiles });

        }

我应该如何解决此问题?

angular upload asp.net-core-webapi primeng
1个回答
0
投票

将以下行插入文件applicationhost.config:

requestLimits maxAllowedContentLength =“ 2097152000” />]

并在控制器中插入以下属性:

[RequestFormLimits(MultipartHeadersLengthLimit = 2097152000)]

[RequestSizeLimit(2097152000)]

© www.soinside.com 2019 - 2024. All rights reserved.