如何在postback后保持FuileUpload控件中的值

简单介绍:

出于安全考虑的设计,Fileupload控件中的值不能被程序设置。

本文给出的解决方案:

1. 选中立即传送到服务端作为临时文件,postback后只是确认处理。

2. 让选中上传文件作为页面的最后一个步骤。之后马上提交。

3. 把文件传送页面和其他选项分成不同的页面(譬如弹出一个窗口专门传文件)

4.假如之前需要验证,多使用js进行客户端验证,阻止postback.

其他部分大家可以自己阅读。

http://www.net-aspect.com/Articles/Retaining-the-Value-of-a-File-Upload-Field-on-Postback.aspx

http://forums.asp.net/p/1075823/1679194.aspx

Retaining the Value of a File Upload Field on Postback

Introduction

The FileUpload control displays a text box control and a browse button that allow users to select a file on the client and upload it to the Web server. The user specifies the file to upload by entering the full path to the file on the local computer (for example, C:/MyFiles/TestFile.txt) in the text box of the control. Alternatively, the user can select the file by clicking the Browse button, and then locating it in the Choose File dialog box.

The FileUpload control does not automatically send a file to the server after the user selects the file to upload. You must explicitly provide a control or mechanism to allow the user to submit the form. Typically, the file is saved or the contents handled in an event-handling method for an event that raises a post back to the server. For example, if you provide a button to submit a file, you could place the code to save the file inside the event-handling method for the click event.

Problem

If you are using a HtmlInputFile control (in ASP.net 1.1) or a FileUpload control (in ASP.net 2.0) the value of the control cannot be retained after any page postback. 

This is done by design, for security reasons to prevent malicious sites from uploading files without permission. Imagine you could alter this value server-side without the user's knowledge or recognition of it, and then upload any file you wanted.  The page could use script to automatically post itself and which has a file upload box prepopulated with something like "c:/mydocuments/myaccounts.mny".

Therefore the value of the file input element cannot be set by the page, only by the user setting it via the Browse button.

In order for any form element in ASP.Net to "retain" its state, the value of the element must be
reset at PostBack. The "value" property of an "input type=file" form field can not be set programmatically, as that would enable a web page to grab any file in the file system on the client without the user's knowledge or consent. Therefore, as HTML will not allow it, ASP.Net can't do it.

The problem may occur if:

  • You do some server side validation on the page when it is submitted and if an incomplete field is detected on the page the file upload control value is blank after the postback
  • You have server controls that are AutoPostback = True. When the postbacks
    are triggered, the value of the file upload box is set to an empty string

Even though the FileUpload has an EnableViewState property in ASP.net 2.0 it is still not possible to keep the value of the file upload field, even if you set EnableViewState to true.

Resolution/Workarounds

There is no genuine resolution to the problem - you simply cannot keep the value of the upload field after postback.  Thus you need to come up with a workaround that rearranges how your form works.

Here are some possible suggested workarounds:

  • Store the file in an temporary location on the server when a postback occurs.  When the user finally commits then do secondary processing after.  You then don't have any files to transfer because they are already server side
  • Get the user to click "upload" before doing anything else, and it at least satisfies their goal.
  • Split the form into an upload page and another page to enter other form details
  • If the problem occurs because you are validating the page server side, you could consider validating it client side using Javascript

Workarounds that won't work:

  • Setting the EnableViewState for the control to true.
  • Placeing postedFile.filename value in a hidden control and then put it back into the HtmlInputFile control on postback.  This won't work because the postedFile.filename and the value properties are read-only
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值