Tuesday, September 18, 2007

Learnings - File uploads does not work during async postbacks


File uploads do not work when doing an async postback. To my knowledge there's no way to support this scenario due to the browser's security restrictions.

Reason - understand how async postbacks are different than a regular postback.

When the browser performs a Regular Postback it has the benefit of being actual code with no security restrictions running on the client computer. When you submit a form it creates a web request with all the form fields. When it encounters an <input type="file"> it examines the file the user chose, reads it from disk, and adds it to the web request.

When the AJAX JavaScript code is running in the browser i.e when Async Postbacks occurs it can do the first part about creating a request with all the form fields. However, when it gets to <input type="file"> fields it doesn't have the necessary security permissions to read those files from disk.

Workaround -

· Have a dedicated "Upload" button that does a regular postback instead of an async postback. You can achieve this using several techniques:

o Have the button be outside all UpdatePanels;

o Have the button be the target of an UpdatePanel's PostBackTrigger; or


<asp:PostBackTrigger ControlID="UploadButton" />


o Call ScriptManager.RegisterPostBackControl(<Button Control>) on it during page load.

if (!IsPostBack)




· Have a dedicated file upload page that doesn't have any UpdatePanels. Many web sites already do this anyway.

Thanks & Regards,

Arun Manglick || Tech Lead

No comments:

Post a Comment