Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 页面未显示,因为请求实体太大。在.NETCore2.1中_Asp.net Core_Iis_.net Core_Asp.net Core Webapi_Asp.net Core 2.1 - Fatal编程技术网

Asp.net core 页面未显示,因为请求实体太大。在.NETCore2.1中

Asp.net core 页面未显示,因为请求实体太大。在.NETCore2.1中,asp.net-core,iis,.net-core,asp.net-core-webapi,asp.net-core-2.1,Asp.net Core,Iis,.net Core,Asp.net Core Webapi,Asp.net Core 2.1,我在通过API将文件上传到服务器时遇到了问题 当我试图从邮递员上传8 Mb大小的图像到服务器时。我第一次尝试上传它,它显示“页面没有显示,因为请求实体太大”。但奇怪的是,“当我上传小尺寸的图片,比如300KB,它会上传,然后尝试上传下一张图片,比如8MB,它会上传,3到5分钟后,我会收到同样的错误 我的上传代码 [Route("api/[controller]")] public class UploadImageController : Controller { private IHo

我在通过API将文件上传到服务器时遇到了问题 当我试图从邮递员上传8 Mb大小的图像到服务器时。我第一次尝试上传它,它显示“页面没有显示,因为请求实体太大”。但奇怪的是,“当我上传小尺寸的图片,比如300KB,它会上传,然后尝试上传下一张图片,比如8MB,它会上传,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");
        }

    }
}
版本:1.0 日期:2019-11-10 05:59:05 字段:日期时间s-ip cs方法cs uri stem cs uri查询s-port cs用户名c-ip cs(用户代理)cs(参考)sc状态sc子状态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 1636

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


我为什么会遇到问题(“由于请求实体太大,页面未显示”) 在我的网站中,SSL设置接受 我只是将它设置为“忽略”,然后它就工作了

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

    <requestLimits maxAllowedContentLength="1073741824" />  

  </requestFiltering>
#Software: Microsoft Internet Information Services 10.0