MVC5将大文件上传到DB System.OutOfMemoryException

时间:2016-11-28 15:02:28

标签: c# asp.net-mvc file-upload

我需要将可能较大的文件上传到我们的网站,然后再上传到数据库中。但是每次我实例化存储文件数据的byte []时,我都会立即获得System.OutOfMemoryException。该框具有足够的RAM,但IIS / .Net似乎对进程内存有限制。

型号:

[
    {
        "Id": "84cc4d2abb57d00fbd67d10c02cc6333a401a7d8bfe4bc15f588da11a834333f",
        "Created": "2016-11-28T14:41:04.757044577Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "--net=host",
            "-e",
            "WORDPRESS_DB_USER=wordpress",
            "-e",
            "WORDPRESS_DB_PASSWORD=secret"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 2,
            "Error": "",
            "StartedAt": "2016-11-28T14:41:05.523186975Z",
            "FinishedAt": "2016-11-28T14:41:05.619181506Z"
        },
        "Image": "sha256:ee397259d4e59c65e2c1c5979a3634eb3ab106bba389acea8b21862053359134",
        "ResolvConfPath": "/var/lib/docker/containers/84cc4d2abb57d00fbd67d10c02cc6333a401a7d8bfe4bc15f588da11a834333f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/84cc4d2abb57d00fbd67d10c02cc6333a401a7d8bfe4bc15f588da11a834333f/hostname",
        "HostsPath": "/var/lib/docker/containers/84cc4d2abb57d00fbd67d10c02cc6333a401a7d8bfe4bc15f588da11a834333f/hosts",
        "LogPath": "/var/lib/docker/containers/84cc4d2abb57d00fbd67d10c02cc6333a401a7d8bfe4bc15f588da11a834333f/84cc4d2abb57d00fbd67d10c02cc6333a401a7d8bfe4bc15f588da11a834333f-json.log",
        "Name": "/dockerpress",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": [
                "/dockermysql:/dockerpress/mysql"
            ],
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "Mounts": [
            {
                "Name": "928da9b62b2adb3dd93905dfd702b766d80f0e7bb6f17b7e7f57f7c6433bdc5d",
                "Source": "/var/lib/docker/volumes/928da9b62b2adb3dd93905dfd702b766d80f0e7bb6f17b7e7f57f7c6433bdc5d/_data",
                "Destination": "/var/www/html",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "84cc4d2abb57",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "no_proxy=*.local, 169.254/16",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "PHPIZE_DEPS=autoconf \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkg-config \t\tre2c",
                "PHP_INI_DIR=/usr/local/etc/php",
                "APACHE_CONFDIR=/etc/apache2",
                "APACHE_ENVVARS=/etc/apache2/envvars",
                "PHP_EXTRA_BUILD_DEPS=apache2-dev",
                "PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2",
                "GPG_KEYS=0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3",
                "PHP_VERSION=5.6.28",
                "PHP_URL=https://secure.php.net/get/php-5.6.28.tar.xz/from/this/mirror",
                "PHP_ASC_URL=https://secure.php.net/get/php-5.6.28.tar.xz.asc/from/this/mirror",
                "PHP_SHA256=07187ba2870f89cef334cd2ad6cb801aeec5eaf283da0293a9a6be75d6786d11",
                "PHP_MD5=1e01c66b2e67ab3b56a6180ee560fe4c",
                "WORDPRESS_VERSION=4.6.1",
                "WORDPRESS_SHA1=027e065d30a64720624a7404a1820e6c6fff1202"
            ],
            "Cmd": [
                "--net=host",
                "-e",
                "WORDPRESS_DB_USER=wordpress",
                "-e",
                "WORDPRESS_DB_PASSWORD=secret"
            ],
            "Image": "wordpress",
            "Volumes": {
                "/var/www/html": {}
            },
            "WorkingDir": "/var/www/html",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "d946a78e9a50fc8fb10873a0c043c9f49a377db44b19edb5e83362b37c025948",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": null,
            "SandboxKey": "/var/run/docker/netns/d946a78e9a50",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "37e08b5c5d5f8a159552b8ab22a0fb37bfbcfd720fd83ce20a983ab85e60157f",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": ""
                }
            }
        }
    }
]

发布行动:

public class CreateFileModel : IFileModel {
...
       [ScaffoldColumn(false)]
       public byte[] FileData { get; set; }
...
}

在模型中实例化接收缓冲区时总会发生错误,该缓冲区将用于更新数据库。

[HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult CreateFile(CreateFileModel model) {
          try {

             if (ModelState.IsValid && Request != null && Request.Files.Count > 0) {

                foreach (string uploadname in Request.Files) {
                    HttpPostedFileBase uploadedFile = Request.Files[uploadname];
                    if (uploadedFile?.ContentLength > 0) {  
                       model.FileData = new byte[uploadedFile.ContentLength];  // <-- BREAKS HERE WITH OutOfMemoryException
                       uploadedFile.InputStream.Read(model.FileData, 0, Convert.ToInt32(uploadedFile.ContentLength));
                    }
                }

                if (model.FileData.Length > 0) {
                    FileMapper mapper = new FileMapper();
                    mapper.Create(model);

                    return RedirectToAction("Index", "Index", new { pucoId = CurrentUser.PUCOID, folderId = CurrentDirectoryId });
                }
             }

          }
          catch (Exception ex) {
             OnError(ex);
          }

          model.FileData = null;
          return View(model);
       }

    }

所以我想弄清楚从哪里开始。我可以使用较小的缓冲区将文件保存到文件系统,但后来我实际上无法将其存入数据库。

你会如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以在IIS中增加应用程序池的专用内存限制大小,这应该向您展示如何执行此操作https://www.404techsupport.com/2016/03/iis-wsus-private-memory/

其次,你的文件有多大?

你能将它们分成较小的文件,然后在服务器端组合一次吗?