FileUpload 控件在回发时丢失其内容

发布于 2025-01-06 12:39:21 字数 153 浏览 5 评论 0原文

我有 3 个 FileUpload 控件。当页面 PostBack 时,它们会丢失内容。当我单击按钮以显示一些隐藏的控件时,页面会回发。

我该怎么做才能在不使用 session 的情况下保留其内容?

I have 3 FileUpload controls. They lose their content when the page PostBack. The page is post back when I click on a button to show some hidden controls.

what can I do to keep their contents without using session ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜兔 2025-01-13 12:39:21

这实际上与安全相关。想象一下,访问一个上传文件的网站,路径中预先填充了“c:\Users\Meensat\passwords.docx”之类的内容。你点击提交,他们就有了你的密码。

您可以在回发时执行的操作是使用 .PostedFile 属性检查文件,如果存在,则将其保存到临时文件中。在Session中,存储对临时文件、文件名等的引用。

EDIT

.PostedFile实际上是FileUpload控件的一个属性。

http://msdn.microsoft.com /en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx

该链接包含示例代码,显示如何将文件保存到磁盘。

This is actually security related. Imagine visiting a site with a file upload and the path pre-filled with something like "c:\Users\Meensat\passwords.docx". You hit submit, and they have your passwords.

What you can do on postback is check for a file using the .PostedFile property, and if one exists, save it to a temp file. In Session, store a reference to the temp file, the file name, etc.

EDIT

.PostedFile is actually a property of the FileUpload control.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx

That link has sample code showing how to save the file to disk.

晚风撩人 2025-01-13 12:39:21

由于 HTTP/Forms 的工作方式,确实没有办法解决这个问题。该文件将在第一次发布时提交到服务器。您还可以将文件字节存储在 ViewState 中,但回发会很慢。您的选择是不执行回发并使用 javascript 或其他替代方法来获得相同的功能或将文件存储在服务器上的某个位置(例如:会话、临时文件夹、数据库)。

There really isn't a way around this due to the way HTTP/Forms work. The file will get submitted on the first post to the server. You could also store the file bytes in ViewState but postbacks will be slow. Your options are to not perform a postback and use javascript or other alternatives to get the same functionality or store the file somewhere on the server (ex: session, temp folder, database).

森末i 2025-01-13 12:39:21

FileUpload控件的内容保存在服务器端。或者你可以在 html 页面中渲染它并使用 CSS 将其隐藏

Save the content of FileUpload control in serverside. or u can render it in the html page and keep it hidden using CSS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文