How to use ASP.NET Code Behind to launch FileUpload

Coreysan 1,806 Reputation points
2025-01-22T01:45:28.4866667+00:00

Is it possible to include an asp:Button with OnClick=DoFileUpload_Click, and the method runs whatever to launch the FileUpload class?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,080 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,582 questions
{count} votes

3 answers

Sort by: Most helpful
  1. SurferOnWww 3,816 Reputation points
    2025-01-22T02:02:21.86+00:00

    Yes, possible.


  2. XuDong Peng-MSFT 11,176 Reputation points Microsoft Vendor
    2025-01-22T08:03:47.9966667+00:00

    Hi @Coreysan,

    It is common to complete file upload through button click event. You can find such code example in the official sample: FileUpload Class - examples.

    the method runs whatever to launch the FileUpload class

    Do you mean you want complete multiple file uploads on one single button click event? If this is the case, you just need to handle the files in FileUpload controls at the same time in click event. Like this:

    protected void Upload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            // Get the name of the file to upload.
            String fileName1 = FileUpload1.FileName;
            // do something else
        }
        if (FileUpload2.HasFile)
        {
            // do some judgments
            String fileName2 = FileUpload2.FileName;
            // something else
        }
    }
    

    But if I misunderstood your requirement, please feel free let me know.

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Albert Kallal 5,496 Reputation points
    2025-01-23T22:54:25.6233333+00:00

    You are 100% correct.

    So, while the server code can’t start an upload?

     

    The client side can “automatically” start an upload. However, the restriction I noted of using code to select a file is NOT possible. In other words, the base requirement of the user having to select the file(s) in question still exists.

     

    Of course, the simple HTML FileUpLoad control provides a rather poor experience. When the user hits submit, and if the file is large?

    Then it “feels” like the browser is frozen while the “whole” file is sent up to the server.

    As a result, it makes sense to adopt a “modern” file upload library.

     

    Those libraries can:

    Provide a drag + drop hot spot in the browser.

    They provide a nice progress bar.

    They up-load the file(s) in small chunks. This allows display of a progress bar, and also allows a cancel button. (And thus, even canceling of a file upload responds instant to the user hitting cancel.

    And, since the file is up-loaded in small chunks?

    Then memory used is small, and load on the server (memory wise) is small.

    And since the file is being up-load in small chunks?

    Then you have no real file limit in terms of the file size being up-loaded.

    Hence, it makes sense (in place of rolling your own) to adopt a nice file upload library and utility.

    There are a good many commercial ones, and also quite a few high quality free ones.

     

     I use the free AjaxFileUploader from the ajax toolkit. That toolkit is for web forms, and has all kinds of nice controls and utilities (such as masked inputs, filtering characters, dialog boxes and more).

     

    So, the file AjaxFile uploader looks like this to select files (you can use the “select file” to launch the file picker dialog, or you can use drag + drop of files into that so called "hot spot".

     

    This example shows using drag + drop of some files:

     upload1

     After you use "select file", (or drag + drop the files), then you can hit the upload button.

    (As noted, these uploaders can also “start” uploading automatic without having to hit “upload”.

     

    Hence, hitting upload?

    I save the files, add a row to a database of files uploaded. And then when the uploading is done, I simply display the table rows into a simple GridView.

    The result is thus this:

     

    upload11

    So, while many attempt to roll their own up-load utility?

    It can be quite a bit of work, hence the suggestion to adopt an existing uploader.

    Hence, all developers should have in their bag of utilities a great file uploading utility. Does not matter which library you adopt, but adopting such libraries will vast enhance any file uploading for your customers.

    In place of the above GridView display, one I suppose could use a tree-view, and display a windows like file explore and hierarchy of files if desired.

     

     

     

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.