Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileUpload control working on second click but not first attempt of saving posted file?

My Question

I managed to answer myself, however the same set of functionality has another problem. For some reason the first postback of the save event of the posted file hits the Ol' Object not set to an instance of an object error, but on the second attempt of uploading a file and firing my save event (converts to byte[] an stored as SQL Server BLOB) it does everything is supposed to do.

Same problem here

There is a good suggestion of using the AJAX AsyncUpload control however I am a firm believer of removing the cause and not treating the problem. I will continue down this route to best my understanding of asp.net etc.

Would there be a wizrd amongst you that could help me identify why I get "object ref not set to inst of obj" error on first postback but on second it works fine. Content page has a master page which wraps content page in an update panel. Not my decision to do this. There is also an update panel with postback triggers targeting my save event.

What are your thoughts people?

like image 361
dotnetnewb Avatar asked Jul 20 '12 08:07

dotnetnewb


People also ask

How do I stop FileUpload control from clearing on postback?

The simple solution for preventing file loss from an upload control on postback is to put the upload control outside of an update panel control on a . aspx page. Or, in other words, put all the input controls that might trigger a postback inside an update panel.

Which property is associated with FileUpload control?

Use the FileName property to get the name of a file on a client to upload by using the FileUpload control. The file name that this property returns does not include the path of the file on the client. The FileContent property gets a Stream object that points to a file to upload.

How do I select multiple files in ASP NET FileUpload?

The FileUpload. AllowMultiple property in . NET 4.5 and higher will allow you the control to select multiple files.


2 Answers

The problem (as seen here http://forums.asp.net/t/1060363.aspx) seems to be when you use the visibility property on the surrounding panel (as it seems you are from the linked question).

The suggested workaround is to use CSS visibility instead so use this to make it invisible -

<asp:Panel ID="pnlUpload" runat="server" class="workerDetailsPanelLeft" style="display:none">

The explanation for this from the thread is

If your container is set to invisible, the upload control is not actually rendered as HTML, causing the form's enctype not to be set to enctype="multipart/form-data", causing the file upload control not to post the selected file back to the server. The workaround is either to make sure the FileUpload control is rendered to HTML (by setting its style to display:none in stead of Visible=false), or by manually setting the enctype

So another workaround would be to alter your form tag to this

<form id="form1" enctype="multipart/form-data" runat="server">

I think either one of those should solve your problem.

like image 172
Kevin Main Avatar answered Sep 27 '22 22:09

Kevin Main


You could do the same thing via code on Page_Load event.. Enter this code and it will solve the issue.

Page.Form.Attributes.Add("enctype", "multipart/form-data");
like image 23
Muhammedh Avatar answered Sep 27 '22 21:09

Muhammedh