ASP.net FileUpload 控件不跨多视图面板维护状态

发布于 2024-11-19 10:17:31 字数 1153 浏览 7 评论 0原文

为了简单起见,我有一个简单的 2 面板多视图。第一个有一个 FileUpload 控件,第二个有一个按钮,用于发送包含文件作为附件的电子邮件。当我在第二个选项卡上检查“FileUpload1.HasFile”时,它返回为 false。如果我返回到视图 1,除了文件上传为空之外,所有其他控件的所有状态都已正确维护。

<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
    <asp:View ID="Step1" runat="server">
      <asp:FileUpload ID="FileUpload1" runat="server" Width="450" EnableViewState="true" />
    </asp:View>
    <asp:View ID="Step2" runat="server">
      <asp:Button ID="btnSubmitForm" runat="server" Text="Submit Data" onclick="btnSubmitForm_Click" />
    </asp:View>
</asp:MultiView>

当用户导航到视图 2 并单击按钮时:

//Initialize smtp server
            SmtpClient smtp = new SmtpClient("myHost");

            //Initialize mail message object
            MailMessage mail = new MailMessage();

//Set all mail message params (to, cc, subject etc...)

//attach file, this is where it fails to recognize the attached document. bool is false :(
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}

有什么想法吗???

For the sake of simplicity, I have a simple 2 panel multiview. On the first there's an FileUpload control and, on the second, a button that sends an email with the file as an attachment. When I check "FileUpload1.HasFile" on second tab it comes back as false. And if I go back to View 1, all states for every other control has been properly maintained except for the fileupload wich is blank.

<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
    <asp:View ID="Step1" runat="server">
      <asp:FileUpload ID="FileUpload1" runat="server" Width="450" EnableViewState="true" />
    </asp:View>
    <asp:View ID="Step2" runat="server">
      <asp:Button ID="btnSubmitForm" runat="server" Text="Submit Data" onclick="btnSubmitForm_Click" />
    </asp:View>
</asp:MultiView>

When user navigates to View 2 and clicks the button:

//Initialize smtp server
            SmtpClient smtp = new SmtpClient("myHost");

            //Initialize mail message object
            MailMessage mail = new MailMessage();

//Set all mail message params (to, cc, subject etc...)

//attach file, this is where it fails to recognize the attached document. bool is false :(
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}

any ideas???

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

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

发布评论

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

评论(1

素年丶 2024-11-26 10:17:31

文件上传被认为是安全敏感的。当用户选择要上传的文件时,表单提交必须在任何其他重定向之前进行。让浏览器允许从状态“恢复”该值可能会导致旧的文件钓鱼时代。这是网站将具有一个带有隐藏文件上传控件和预填充值的表单的地方。当用户提交表单时,浏览器也会上传该文件(如果存在)。

现代安全标准阻止了这些尝试。如果您希望用户上传文件,则无法导航到新页面。您可以重新设计以在一个页面上执行所有操作,或者在页面导航期间获取上传的文件,将其存储到某个临时位置,将唯一的 ID 返回到新页面,并在提交该页面时将两者绑定在一起。并对点击返回或不继续第二页的用户进行清理。

File Uploads are considered security sensitive. When a user picks a file to upload, the form submit will have to take place before any other redirects. To have the browser allow the value to be "restored" from state could lead to the old days of file fishing. This is where a site would have a form with a hidden file upload control with a pre-populated value. When the user submitted the form, the browser would upload the file too, if it existed.

Modern security standards block these attempts. If you want the user to upload a file, you can't navigate to a new page. You can either redesign to do everything on one page, or take the uploaded file during page navigation, store it to some temp location, return a unique id to the new page and on submission of that page tie the two together. And implement a cleanup for people who click back or just don't proceed with the second page.

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