gpt4 book ai didi

c# - 我应该处理 FileUpload.PostedFile.InputStream 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:51 25 4
gpt4 key购买 nike

是否需要这段代码,或者因为页面的 UnLoad 事件无论如何都会处理页面上的所有控件,所以编写这样的代码毫无意义:

fu.PostedFile.InputStream.Flush();
fu.PostedFile.InputStream.Close();
fu.FileContent.Dispose();

我问是因为来自 msdn 的例子我看到他们写的代码是这样的

FileUpload1.SaveAs(savePath);

但之后不要费心处理流,但在手头上我看到有些人确实在保存后明确处理输入流?

最佳答案

Official guidance建议不需要处理此流,因为它将在请求处理结束时处理:

Server resources that are allocated to buffer the uploaded file will be destroyed when the request ends. To save a durable copy of the file, use the SaveAs method.

为了支持这一点,我还通过源代码挖掘了一些代码。结果是 FileUploadHttpPostedFile 都不负责处理这个流。事实上,它们本身根本不持有任何资源,只是提供一个接口(interface)来访问部分请求。

HttpRequest 做了一些处理。但并不是所有的一次性元素都会被处理掉。这是它的Dispose:

/*
* Cleanup code
*/
internal void Dispose() {
if (_serverVariables != null)
_serverVariables.Dispose(); // disconnect from request

if (_rawContent != null)
_rawContent.Dispose(); // remove temp file with uploaded content
}

但是没有配置的是上传控制接口(interface)集合HttpRequest.Files。为了用数据填充这个集合,每个发布的文件都被包装到一个 HttpPostedFile 对象中,并为每个创建一个 HttpInputStream。这个流对象持有对整个数据的引用(参见上面的 rawContent)并且知道相关文件部分的偏移量和长度。值得注意的是,HttpInputStream 实现了 IDisposable,但是我找不到处理这些流对象的代码。

总结一下:

  1. 上传控件不处理流
  2. Request 或 HttpContext 也不处理流
  3. 但是 Request 处理基础数据

所以看起来这里的想法是当请求处理完成时,将删除对相关数据的引用并将其处理掉。因此,手动处置您使用过的流不会有什么坏处,但这也不是必需的。

关于c# - 我应该处理 FileUpload.PostedFile.InputStream 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37139995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com