3

I have a file upload inside the update panel, and an upload button that uploads the file to the server. Is it possible to upload the file without clicking the upload button? I want to remove the upload button and upload the file as soon as the file is selected from the user's machine. Or, have a 4 second timer, then call the upload_click to check if the fileupload has a file or not. How can I do it without a button inside update panel?

<asp:UpdatePanel ID="UP_DDL" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
    <ContentTemplate> 
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Upload" OnClick="Upload_Click" runat="server" Text="Upload" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="Upload"/>
    </Triggers>
</asp:UpdatePanel>

protected void Upload_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        //create the path to save the file to     
        string fileName = Path.Combine(Server.MapPath("~/Bulk Upload"), FileUpload1.FileName);
        //save the file to our local path  
        FileUpload1.SaveAs(fileName);
    }
}
0

3 Answers 3

1

I am sure you can use any of the events on the HTML INPUT File element to fire up a full post back, which is all you need in order to upload the file automatically.

Google, at least on the standard interface, uses some sort of Flash plugin to accomplish what you want.

There might be some other jQuery plugins that provide this functionality out of the box. This one, for example, seems to do it.

Sign up to request clarification or add additional context in comments.

Comments

0

You would need to mimic the click with some client-side code, such as using submit from Javascript - and this is not supported by at least one notable browser, I believe, IE.

Otherwise, you could use jQuery ajax calls to static web methods which do uploads on the fly, behind the scenes, when the value of the file upload control have changed.

Comments

0

You can hide Upload button with display:none inline style and add following code to Page_PreRender method:

FileUpload1.Attributes["onchange"] = ClientScript.GetPostBackClientHyperlink(Upload, "");

Or consider to use AsyncFileUpload control from AjaxControlToolkit

3 Comments

How can I just update the update panel so that the whole page doesnt refresh
You can't do this with standard FileUpload control since it requires full postback.
I have tried to call __doPostBack('<%= btnUpload.ID %>', ""); in my javascript in onchange event of FileUpload Control. I do get the postback event but the PostedFile property is null, and HasFile = false. It works fine the second time I select a file!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.