Seems like an obvious question but I'm still confused. I have an asp FileUpload control on the page which also happens to be inside of an UpdatePanel. I have the UpdatePanel set to UpdateMode="Conditional". I also have a postback trigger (not an asyncpostback trigger) set in the triggers section. On button click, it is supposed to upload the file to a database, handled in my datalayer. On the first attempt, it will fail as the PostedFile is null, but you see the page perform a postback (which inherently clears/resets the download control). When I try uploading a file again, it is fine. My first thought was because it needed to postback in order to actually grab the bytes, right? Well ok, so under that logic, I figured if I attempted to upload a different file, then it would actually upload the previous file. It did not; it uploaded the new file. So that's where I'm confused.
Here's the upload method:
protected void ReplaceDoc()
{
var data = ulReplaceDoc.FileBytes;
var fileName = ulReplaceDoc.PostedFile.FileName;
if (MocApi.ReplaceWorkingDocument(_request.MocRequestID, _currentUser, fileName, data))
{
//refresh files
ShowFiles();
}
}
Pretty straight forward. It is supposed to grab the file, upload it (which runs an UPDATE query on the database to change the file), then it refreshes a datasource to show the new file.
So my question is this: Why does it only fail on the first try? Also, what would be a better way to achieve this effect?
In Page Load Event, add the following code,
It will work. The enctype attribute is missing during first time PostBack.