由于请求实体太大,因此未显示该页面。 .Net Core 2.1中的

时间:2019-11-10 08:25:36

标签: asp.net-core iis .net-core asp.net-core-webapi asp.net-core-2.1

我在通过API将文件上传到服务器时遇到了问题 当我尝试将图像大小8 Mb从Postman上传到服务器时。我第一次尝试上传它,显示为“由于请求实体太大,因此未显示该页面”。但是奇怪的是,“当我上传300 Kb的小尺寸图片时,它会上传并尝试上传下一张图片,例如8 MB,它会上传,而3或5分钟后我会收到相同的错误。

我的上传代码

[Route("api/[controller]")]
public class UploadImageController : Controller
{
    private IHostingEnvironment _environment;
    public UploadImageController(IHostingEnvironment environment)
    {
        _environment = environment;
    }
    internal void IsExists(string filepath)
    {
        if (!Directory.Exists(_environment.WebRootPath + "\\media"))
        {
            Directory.CreateDirectory(_environment.WebRootPath + "\\media");
        }
        if (!Directory.Exists(_environment.WebRootPath + "\\media\\" + filepath))
        {
            Directory.CreateDirectory(_environment.WebRootPath + "\\media\\" + filepath);
        }
    }
    internal string GetNewFileName(string filenamestart, string fullname)
    {
        Char delimiter = '.';
        string fileExtension;
        string strFileName = string.Empty;
        strFileName = DateTime.Now.ToString().
            Replace(" ", string.Empty).
            Replace("/", "-").Replace(":", "-");
        fileExtension = fullname.Split(delimiter).Last();
        Random ran = new Random();
        strFileName = $"{ filenamestart}_{ran.Next(0, 100)}_{strFileName}.{fileExtension}";
        return strFileName;
    }
    private bool IsValidExtension(IFormFile filename)
    {
        bool isValid = false;
        Char delimiter = '.';
        string fileExtension;
        string[] imgTypes = new string[] { "png", "jpg", "gif", "jpeg" };
        fileExtension = filename.FileName.Split(delimiter).Last();
        // fileExtension = substrings[substrings.Length - 1].ToString();
        int fileType = 0;
        if (imgTypes.Contains(fileExtension.ToLower()))
        {
            fileType = 1;
        }

        switch (fileType)
        {
            case 1:
                if (imgTypes.Contains(fileExtension.ToLower()))
                {
                    isValid = true;
                }
                break;
            default:
                isValid = false;
                break;
        }
        return isValid;
    }
    [HttpPost]
    // GET: api/<controller>
    [Route("UploadapiImage")]
    [RequestFormLimits(MultipartBodyLengthLimit = 209715200)]
    [RequestSizeLimit(209715200)]
    public async Task<IActionResult> UploadapiImage()
    {
        try
        {

            var Files=Request.Form.Files;
            if (Files.Count == 0)
            {
                return Ok(Json(""));
            }
            IsExists("Mobile");
            //var file = Convert.FromBase64String(objUploadModel.File);
            string uploads = _environment.WebRootPath + "\\media\\Mobile";
            int i = 0;
            string[] strFileNames = new string[Files.Count];
            foreach (var file in Files)
            {
                if (IsValidExtension(file))
                {
                    string imagename = GetNewFileName("Mobile", Files[i].FileName);
                    string fullpath = uploads + "\\" + imagename;
                    using (FileStream fileStream = System.IO.File.Create(fullpath))
                    {
                        await Task.Run(() => Files[i].CopyToAsync(fileStream));
                        fileStream.Flush();
                        fileStream.Close();
                    }

                    strFileNames[i] = "\\media\\Mobile\\" + imagename;
                }
                else
                {
                    strFileNames[i] = "";
                }
                i++;
            }
            return Ok(strFileNames);

        }
        catch (Exception ex)
        {
            return Ok("Exception from Api");
        }

    }
}

在Startup.cs

services.Configure<FormOptions>(x =>
        {
            x.ValueLengthLimit = int.MaxValue;
            x.MultipartHeadersLengthLimit = int.MaxValue;
            x.MultipartBodyLengthLimit = int.MaxValue;
        });

在Web Config中。

<requestFiltering>

    <requestLimits maxAllowedContentLength="1073741824" />  

  </requestFiltering>

我尝试将日志保留在服务器和IIS中,但未找到任何信息。 在IIS日志中写入

#Software: Microsoft Internet Information Services 10.0

版本:1.0

日期:2019-11-10 05:59:05

字段:日期时间s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent)cs(Referer)sc-status sc-substatus sc- win32状态耗时

2019-11-10 05:59:05 10.0.0.4 POST / api / UploadImage / UploadapiImage-443-27.34.104.230 Mozilla / 5.0 +(Windows + NT + 10.0; + Win64; + x64)+ AppleWebKit / 537.36 +(KHTML,+ like + Gecko)+ Chrome / 78.0.3904.97 + Safari / 537.36-413 0 0 1636

服务器上的IIS版本显示在图像上。

enter image description here

1 个答案:

答案 0 :(得分:0)

为什么我遇到问题(“因为请求实体太大,所以未显示该页面”) 在我的网站中, SSL设置接受  我只是将其设置为忽略,然后就可以使用了。

enter image description here