在保存文件之前使用 MultipartFormDataStreamProvider 检测文件大小?

cod*_*fun 1 file-upload asp.net-web-api asp.net-web-api2

我们正在使用MultipartFormDataStreamProvider来保存客户端上传的文件。我有一个硬性要求,即文件大小必须大于 1KB。最简单的事情当然是将文件保存到磁盘,然后查看文件,不幸的是我不能这样做。将文件保存到磁盘后,我无法访问它,因此我需要在将文件保存到磁盘之前查看该文件。我一直在查看流提供程序的属性,试图找出文件的大小,但不幸的是我没有成功。

我使用的测试文件是 1025 字节。

MultipartFormDataStreamProvider.BufferSize 为 4096
Headers.ContentDisposition.Size 为 null
ContentLength 为 null

有没有办法在将文件保存到文件系统之前确定文件大小?

cod*_*fun 5

感谢Guanxi,我能够制定一个解决方案。我在链接中使用了他的代码作为基础,我只是添加了更多 async/await 优点:)。我想添加解决方案,以防万一它可以帮助其他人:

    private async Task SaveMultipartStreamToDisk(Guid guid, string fullPath)
    {
        var user = HttpContext.Current.User.Identity.Name;

        var multipartMemoryStreamProvider = await Request.Content.ReadAsMultipartAsync();

        foreach (var content in multipartMemoryStreamProvider.Contents)
        {
            using (content)
            {
                if (content.Headers.ContentDisposition.FileName != null)
                {
                    var existingFileName = content.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);

                    Log.Information("Original File name was {OriginalFileName}: {guid} {user}", existingFileName, guid,user);

                    using (var st = await content.ReadAsStreamAsync())
                    {
                        var ext = Path.GetExtension(existingFileName.Replace("\"", string.Empty));
                        List<string> validExtensions = new List<string>() { ".pdf", ".jpg", ".jpeg", ".png" };
                        //1024 = 1KB
                        if (st.Length > 1024 && validExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase))
                        {
                            var newFileName = guid + ext;
                            using (var fs = new FileStream(Path.Combine(fullPath, newFileName), FileMode.Create))
                            {
                                await st.CopyToAsync(fs);
                                Log.Information("Completed writing {file}: {guid} {user}", Path.Combine(fullPath, newFileName), guid, HttpContext.Current.User.Identity.Name);
                            }
                        }
                        else
                        {
                            if (st.Length < 1025)
                            {
                               Log.Warning("File of length {FileLength} bytes was attempted to be uploaded: {guid} {user}",st.Length,guid,user);
                            }
                            else
                            {
                                Log.Warning("A file of type {FileType} was attempted to be uploaded: {guid} {user}", ext, guid,user);
                            }
                            var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest)
                            {
                                Content =
                                    st.Length < 1025
                                        ? new StringContent(
                                            $"file of length {st.Length} does not meet our minumim file size requirements")
                                        : new StringContent($"a file extension of {ext} is not an acceptable type")
                            };
                            throw new HttpResponseException(responseMessage);
                        }
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)