I have an aspx page with <input type='file'>
elements. I want to use C# to save the files uploaded via these controls. I've seen certain solutions (like this one), which I can use in On_Load
. Problem is my input
controls will be created dynamically using JS, and I don't want a postback each time an image is uploaded.
Is there some equivalent method in C# to JS's .on()
method, so that I can attach a handler to future controls?
EDIT: As suggested by @Oceans, I added an onchange
event handler inside the input
control:
<input type="file" onchange="saveFile" runat="server">
And I have a saveFile
handler in my .cs
behind:
protected void saveFile(object sender, EventArgs e)
{
var imagesDir = Server.MapPath("Images/");
var uploader = sender as FileUpload;
uploader.PostedFile.SaveAs(imagesDir + uploader.PostedFile.FileName);
}
When I choose a file via the input
control the function saveFile
is not ran. I replaced onchange="saveFile"
with onchange="alert('hi')"
to check if the event is being fired and it is, so I'm obviously missing something... probably something to do with postback
and .asp
page cycle?